Oop - Midterm 2022 - With - Answers
Oop - Midterm 2022 - With - Answers
Id:
Program: Day class □ Night class □
Considering UML in Question 2, create three car objects, each using a different constructor in the main method.
Also, create a factory object. Call the printAllCarsInfo() method which prints all the cars in the factory. getCarInfo() int
Car class method should return attributes of a car.
In setYear method, year argument/parameter cannot be bigger than 2022. If it is, throw an exception with a message. You
should handle the exception in the main method.
Ayrica, bir tane de factory nesnesi oluşturun. Factory’deki tüm car nesnelerini ekrana yazdıran printAllCarsInfo() metodunu
çağırın. Car classindaki getCarInfo() car nesnesinin tüm özelliklerini döndürmelidir.
setYear metodunda year argümanı/parametresi 2022'den büyük olamaz. Eğer olursa, bir mesajla istisna (exception) firlatılmak
zorundadır. İstisnayı main metodda ele almalısınız (handle).
QUESTION 4. [24p]: Write outputs in the given table. All the classes are declared in the same package.
Çıktıları verilen tabloya yazın. Tüm sınıflar aynı pakette yazılmıştır.
GOOD LUCK!
Q2-Q3
Factory(Car[] cars) {
this.cars = cars;
}
void printAllCarInfos(){
for(Car car: cars)
System.out.println(car.printCarInfo());
}
}
Q1.
enum Position{
PROGRAM(100),
CONSULTANT(200),
DESIGNER(300);
private final int id;
//1- Must have constructor
public int getId(){
return id;
}
}
//2- file name should have same name as the public class, also we can’t have 2 public
public class Employee{
private int empId;
private String name;
public static final String companyName="Google";
public Employee(String name, int empId,String cName){
this.name=name;
this.empId= empId;
this.companyName= cName;//3- cannot assign a value to a final variable
}
public String getName(){
return this.name;
}
public String getId(){
return this.empId; //4- should have same return datatype
}
static void printName(){
//5- non static variable cannot be referenced from a static method
System.out.println(" Name:"+name);
}
@Override
public String toString(){
return this.empId+": name:"+this.name;
}
}
public class Company {
public static void main(String[] args) {//6- main method should be static
Employee employee1= new Employee("Ahmed", 100, "Google");
System.out.println(employee1);
employee1.name="Lana";// 7- name has a private access
Employee employee2= new Employee("Omer", 200, "Microsoft");
System.out.println(employee2.getName());
Employee.getName();//8- non static method cannot be referenced from a static method
Employee employee3= new Employee("Ali", 300.5f, "Meta");//9- incompatible type, passing float value to int
System.out.println(employee4);// 10- object not exist
}
}