0% found this document useful (0 votes)
9 views12 pages

System - Out.println ("Welcome To NTU")

Uploaded by

Zhixuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views12 pages

System - Out.println ("Welcome To NTU")

Uploaded by

Zhixuan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

2022 S1

Question 1
a) What do you interpret about System and out in the statement below?
System.out.println("Welcome to NTU");

Answer

System is a class in the java.lang package that provides access to the standard input, output and

error streams.

out is a public static member field of the System class and is an instance of PrintStream type.

b) List THREE differences between method overloading and method overriding.

Answer

Method Overloading Method Overriding

Method overloading is a compile-time polymorphism. Method overriding is a run-time polymorphism.

It is used to grant the specific implementation of the


It helps to increase the readability of the program. method which is already provided by its parent class or
superclass.
It is performed between two classes with inheritance
It occurs between methods of the same class.
relationships. (Superclass and subclass)

Method overloading may or may not require


Method overriding always needs inheritance.
inheritance.

In method overloading, methods must have the


In method overriding, methods must have the same
same name and different signatures (different
name and same signature.
number of parameters and parameter types).
In method overloading, the return type can or can
In method overriding, the return type must be the same or
not be the same, but we just have to change the
co-variant.
parameter.
Static binding is being used for overloaded
Dynamic binding is being used for overriding methods.
methods.
It gives better performance. The reason behind this is that
Poor Performance due to compile time
the binding of overridden methods is being done at
polymorphism.
runtime.

Private and final methods can be overloaded. Private and final methods can’t be overridden.

Argument list should be different while doing method


Argument list should be same in method overriding.
overloading.

c) Explain the object-oriented feature Encapsulation/Information hiding. List TWO benefits of this
feature. Elaborate how this feature is achieved in Java programming.

Answer

Encapsulation builds a barrier to protect an object’s private data.

2022 S1 1
Information hiding hides the details or implementation of the class from users, users only need to
know what a class does and how to call the methods of the class.

Benefits

Protect what you have

Protect your data

Encapsulation and Information Hiding in Java

Define class attributes as private.

Access these private attributes using accessors (get methods) and mutators (set methods).

d) List THREE differences between static method and instance method.

Answer

Static Method Instance Method

Can be called without creating an object of class Cannot be called without creating an object of class

Can only access static members of the class Can access both static and instance members of the class

Cannot be overridden Can be overridden by subclasses

Cannot use the this keyword as there is no


Can use the this keyword
instance for ‘this’ to refer to.

Question 2
Study the partial UML class diagram in Figure Q2.

a) Draw the COMPLETE class diagram by adding the following CLASSES (with their necessary
methods) into the partial diagram in Figure Q2:

ClassC implements InterfaceB , and provides implementation for all the necessary methods.

ClassD inherits ClassA , and contains only one method with implementation for the print(s:

String) method.

ClassE inherits ClassA , and implements InterfaceB . It provides implementation for the print(x:

int, y: int) method. It does not contain any other method.

ClassF inherits ClassD , and provides implementation for all the necessary methods.

ClassG inherits ClassE , and provides implementation for all necessary methods.

2022 S1 2
Answer

b) Write the Java code for all the CLASSES in the complete class diagram and their methods. Note
that the print methods in all the classes simply print out the values of their parameters.

Answer

public abstract class ClassA{


public abstract void print(String s);
public abstract void print(String s, int x);
public void print(int x, String s){
System.out.println("x: " + x + "s: " + s);
}
}

public interface InterfaceB{


public void print(int x, int y);
//public abstract void print(int x, int y);
//can don't need abstract keyword since it is an interface
public void print(int x, String s);
}

public class ClassC implements InterfaceB{


public void print(int x, int y){
System.out.println("x: " + x + "y: " + y);
}
public void print(int x, String s){
System.out.println("x: " + x + "s: " + s);

2022 S1 3
}
}

public abstract class ClassD extends ClassA{


public void print(String s){
System.out.println("s: " + s);
}
}

public abstract class ClassE extends ClassA implements InterfaceB{


public void print(int x, int y){
System.out.println("x: " + x + "y: " + y);
}
}

public class ClassF extends ClassD{


public void print(String s, int x){
System.out.println("s: " + s + "x: " + x);
}
}

public class ClassG extends ClassE{


public void print(String s){
System.out.println("s: " + s);
}
public void print(String s, int x){
System.out.println("s: " + s + "x: " + x);
}
}

c) Explain the outcome, the class’s method called or used (if any) and the type of casting (if any)
for each line of the following codes during compile-time and runtime.

ClassA a = new ClassG();


a.print("100", 100);
ClassE d = (ClassE)a;
d.print("100", 100);

Answer

Line Explanation
ClassA a = new ClassG(); Upcasting from ClassG to ClassA (compile OK, runtime OK)
a.print("100", 100); Use the method in ClassG (compile OK, runtime OK)

Downcasting (compile OK) Runtime OK since a is an object of


ClassE d = (ClassE)a;
ClassG, and ClassG to ClassE is upcasting
d.print("100", 100); Use the method in ClassG (compile OK, runtime OK)

Question 3

2022 S1 4
a) Study the following description of a simplified Final Year Project (FYP) system.

FYP is an individual work.

Project information only contains projectID, supervisorID, studentID, projectTitle and area.

The projects fall into one of the following three areas: Hardware, Software, and research. A
project only belongs to one area.

A project is created by a faculty. Once a faculty submits a new project title to the FYP system,
the supervisorID will be the faculty’s userID and projectID will be an automatically generated
sequential index.

The users can login to the FYP system using userID and password.

The users include faculty, students, and FYP coordinator, who is a faculty as well.

A student can view the detailed information of his/her registered project.

A supervisor can modify the title and area of all the projects under his/her supervision.

Only the FYP coordinator can assign a project to a student.

Only the FYP coordinator can change the supervisor of a project. When a supervisor is leaving
NTU, the FYP coordinator will find a faculty in the same area with a light workload to be the
replacement supervisor.

The users can communicate via Email, Teams, and Phone calls.

You are tasked to identify the classes needed to build the application based on the description
above.

Show your design with a Class Diagram. Your Class Diagram should show clearly the
relationships between classes, enumeration, relevant attributes, necessary class methods, logical
multiplicities, meaningful role names, association names, and constraint(s), if any.

Answer

2022 S1 5
b) Explain your application of TWO of the SOLID design principles in your class diagram design in
3(a).

Answer

Single Responsibility Principle (SRP)

According to SRP, a class should have only one reason to change.

In the class diagram, each class has a single responsibility. For example, the User class is
responsible for holding the userID and password attributes and allowing users to login to the
system. The Faculty class is responsible for supervising projects and the Project class is
responsible for holding project information. The Student class is responsible for holding student
information, and the FYPCoordinator class is responsible for assigning projects to students and
changing supervisors.

By ensuring that each class has a single responsibility, the class diagram design follows SRP.

Liskov Substitution Principle (LSP)

2022 S1 6
LSP states that objects of a superclass should be replaceable with objects of its subclasses
without affecting the correctness of the program. In other words, the subclasses should be able to
be used interchangeably with their parent class.

The Faculty class is a subclass of the User class, and it should be able to be used in place of the
User class without affecting the correctness of the program. The same goes for the Student class,
which is also a subclass of the User class. The FYPCoordinator class inherits from the Faculty
class and the FYPCoordinator class can do everything that the subclass can do.

Question 4
The UML Class Diagram in Figure Q4 shows the relationships of THREE classes: LibraryItem class,
Book class, and Newspaper class.
Study carefully the class diagram and the details depicted.

Assume there is only one book and one newspaper in the library at this moment.

Please write a C++ code for the simple library system.

Write C++ code for the THREE classes.

Separate the declaration code and implementation file/s (.cpp) for the implementation
code.

Write the libraryMain.cpp for a complete implementation.

A sample run of the library is shown below:

Enter libraryItems:

Enter Book Author Name:


William Tan
Enter Book Title Name: C++

2022 S1 7
Enter Newspaper Date: 2022-09-27
Enter Newspaper Title Name: Straits Times

MENU
1. Borrow Book
2. Return Book
3. Borrow Newspaper
4. Return Newspaper
5. Exit

Enter your choice: 1


Book Title: C++ Author: William Tan
Borrow Item successfully.

MENU
1. Borrow Book
2. Return Book
3. Borrow Newspaper
4. Return Newspaper
5. Exit

Enter your choice: 1


Book Title: C++ Author: William Tan
Item is on loan.

MENU
1. Borrow Book
2. Return Book
3. Borrow Newspaper
4. Return Newspaper
5. Exit

Enter your choice: 2


Book Title: C++ Author: William Tan
Thank you for returning the Item.

MENU
1. Borrow Book
2. Return Book
3. Borrow Newspaper
4. Return Newspaper
5. Exit

Enter your choice: 4


Newspaper title: Straits Times Date: 2022-09-27
The Item is not on loan.

MENU
1. Borrow Book
2. Return Book
3. Borrow Newspaper
4. Return Newspaper
5. Exit

Enter your choice: 6

Invalid Choice Entered

MENU
1. Borrow Book
2. Return Book
3. Borrow Newspaper
4. Return Newspaper
5. Exit

Enter your choice: 5

Thank you for using the library system.

2022 S1 8
Answer

Remember the colon after using namespace std;

Use getline(cin, variable) to read in string until newline character

Can don’t include guard in header files

Remember to include header files of the superclasses

LibraryItem.h

#ifndef LIBRARYITEM_H
#define LIBRARYITEM_H

#include <iostream>
#include <string>

using namespace std;

class LibraryItem {
private:
string _title;
bool _availability = true;

public:
LibraryItem(string title);
virtual void displayInfor() = 0;
void borrowItem();
void returnItem();
string getTitle();
void setTitle(string title);
bool getAvailability();
void setAvailability(bool avail);
~LibraryItem();
};

#endif // LIBRARYITEM_H

Book.h

#ifndef BOOK_H
#define BOOK_H
#include <string>
#include "LibraryItem.h"

using namespace std;

class Book : public LibraryItem {


private:
string _author;

public:
Book(string title, string author);
void displayInfor();
~Book();

};

#endif // BOOK_H

Newspaper.h

2022 S1 9
#ifndef NEWSPAPER_H
#define NEWSPAPER_H
#include <string>
#include "LibraryItem.h"

using namespace std;

class Newspaper: public LibraryItem {


private:
string _date;

public:
Newspaper(string title, string date);
void displayInfor();
~Newspaper();
};

#endif // NEWSPAPER_H

LibraryItem.cpp

#include <string>
#include <iostream>
#include "LibraryItem.h"

using namespace std;

LibraryItem::LibraryItem(string title){
_title = title;
}

void LibraryItem::borrowItem(){
displayInfor();
if (getAvailability() == true){
cout << "Borrow Item successfully." << endl;
setAvailability(false);
}
else{
cout << "Item is on loan." << endl;
}
}

void LibraryItem::returnItem(){
displayInfor();
if (getAvailability() == false){
cout << "Thank you for returning the Item." << endl;
setAvailability(true);
}
else{
cout << "The item is not on loan." << endl;
}
}

string LibraryItem::getTitle(){
return _title;
}

void LibraryItem::setTitle(string title){


_title = title;
}

bool LibraryItem::getAvailability(){
return _availability;
}

void LibraryItem::setAvailability(bool avail){


_availability = avail;
}

2022 S1 10
LibraryItem::~LibraryItem(){}

Book.cpp

#include <string>
#include <iostream>
#include "LibraryItem.h"
#include "Book.h"

using namespace std;

Book::Book(string title, string author): LibraryItem(title){


_author = author;
}

void Book::displayInfor(){
cout << "Book Title: " << getTitle() << " Author: " << _author << endl;
}

Book::~Book(){}

Newspaper.cpp

#include <string>
#include <iostream>
#include "LibraryItem.h"
#include "Newspaper.h"

using namespace std;

Newspaper::Newspaper(title, date): LibraryItem(title){


_date = date;
}

void Newspaper::displayInfor(){
cout << "Newspaper Title: " << getTitle() << " Date: " << _date << endl;
}

Newspaper::~Newspaper(){}

libraryMain.cpp

#include <string>
#include <iostream>
#include "LibraryItem.h"
#include "Book.h"
#include "Newspaper.h"

using namespace std;

int main() {

cout << "Enter libraryItems: " << endl;


cout << endl;

int choice = 0;
string author;
string bookTitle;
string date;
string newsTitle;

cout << "Enter Book Author Name: " << endl;


getline(cin, author);

2022 S1 11
cout << "Enter Book Title Name: " << endl;
getline(cin, bookTitle);
Book *book = new Book(bookTitle, author);
cout << "\nEnter Newspaper Date: " << endl;
getline(cin, date);
cout << "Enter Newspaper Title Name: " << endl;
getline(cin, newsTitle);
Newspaper *newspaper = new Newspaper(newsTitle, date);

do{
cout << "\n MENU" << endl;
cout << "1. Borrow Book" << endl;
cout << "2. Return Book" << endl;
cout << "3. Borrow Newspaper" << endl;
cout << "4. Return Newspaper" << endl;
cout << "5. Exit" << endl;
cout << "\nEnter your choice: ";
cin >> choice;

switch(choice){
case 1:
book->borrowItem();
break;
case 2:
book->returnItem();
break;
case 3:
newspaper->borrowItem();
break;
case 4:
newspaper->returnItem();
break;
case 5:
cout << "\nThank you for using the library system." << endl;
break;
default:
cout << "\nInvalid Choice Entered" << endl;
break;
}
} while (choice != 5);

return 0;
}

2022 S1 12

You might also like