You are on page 1of 4

Name: SCORE Q1 Q2 Q3 Q4 TOTAL

Id:
Program: Day class Night class

KARABUK UNIVERSITY | COMPUTER ENGINEERING DEPARTMENT


Object Oriented Programming | Midterm | Fall | 6.11.2019 | 9.00 | Duration: 90 mins

QUESTION 1[30pt]: Write necessary classes and enum types for given following UML diagram. All the
classes are declared in the same package.
a) Create Animal, Zoo Classes and
AnimalType according to the given
UML class diagrams.

b) In the main method, create 4 objects


from Animal class, each will be different
animal type. Put them all in a array of
type Animal called “animals”.

c) Create a myZoo object. Print all


animals in the zoo using printAnimal()
method.

[a) Verilen UML sınıf diyagramlarına göre


Animal ve Zoo Sınıflarını ve AnimalType
enum-ını oluşturun.
b) Main Metodda, her biri farklı
AnimalTypedan olmak uzere 4 Animal nesnesi
oluşturun. Hepsini “animals” adında bir
Animal dizisine koyun.
c) myZoo nesnesi oluşturun. printAnimal ()
metodunu kullanarak Zoo’daki tüm
Animal’lari yazdırın.]

QUESTION.2[30pt]: Write necassary lines of code to perform followings

a. Create a class called EncapsulatedStudent, which applies data encapsulation (data hiding) to
Student class.

public class Student {


public int midterm; // midterm cannot be less than 0 or greater than 100
public int finalExam; // finalExam cannot be less than 0 or greater than 100
}

b. EncapsulatedStudent class should throw an exception (with an error message) if any illegal
arguments (midterm and finalExams) are set.

c. Inside the MainClass, create an instance (student1) from EncapsulatedStudent, set attributes for it,
handle any exception, and print the error message on the screen. Use getAverage() method to return the
average score, then print it on the screen. [averageScore= midTerm *0.4 + finalExam*0.6;]

Question.2
a. Student 'a data encapsulation (kapsülleme, gizleme) prensibini uygulayan EncapsulatedStudent adlı bir class oluşturun.

b. Illegal bir arguman ( midterm veya finalExams icin) girilirse, EncapsulatedStudent sınıfı bir exception (bir hata mesajıyla)
firlatmalidir.
c. MainClass’ta EncapsulatedStudent sinifindan bir student1 nesnesi (instance) oluşturun ve degiskenleri girin, herhangi bir exception
durumuyla başa çıkın (handle edin) ve hata mesajını ekrana yazdırın. Ortalama puanı döndürmek için getAverage() metodunu kullanın,
ardından ekrana yazdırın. [averageScore= midTerm *0.4 + finalExam*0.6;]
QUESTION.3 [20p]: Write outputs in the given table. All the classes are declared in the same package.
public class Worker { public class MainClass {
String name; public static void main(String[] args) {
public static String compName,
public static int number; Worker w1 = new Worker("Ahmet");
public int id; System.out.println(Worker.compName); //(1)

public Worker(String n){ Worker w2 = new Worker("Mert");


this(n, number); Worker w3 = new Worker("Cenk");
compName ="Iron and Steel Inc."; System.out.println(Worker.number); //(2)
} Worker w4 = new Worker("Okan");
System.out.println(w2.id); //(3)
private Worker(String n, int num){
number = num; Company comp = new Company(4);
number++; comp.addWorker(w1);
this.name =n; comp.addWorker(w2);
this.id =number; comp.addWorker(w3);
} comp.addWorker(w4);
} //---------------------------------------------
public class Company { System.out.println(Company.index); //(4)
static int index; Worker.compName ="Just Steel Inc.";
Worker[] workers; comp.showData(w3); //(5)
}
public Company(int num){ }
workers = new Worker[num];
} (1).
Iron and Steel Inc.
void addWorker(Worker w){ (2). 3
workers[index] = w;
index++; (3). 2
}
(4). 4
public void showData(Worker w){
System.out.println(w.id +":"+w.compName); (5). 3:Just Steel Inc.
}
}
QUESTION.4 [20pt]: Write down the output of the following lines of code:
enum Level{
HIGH(300, "dangerous"),
MEDIUM(200, "okay"),
LOW(100, "too easy");
Write Your Answers Here:
int height;
private String desc;
HIGH:dangerous

private Level(int h, String d) { MEDIUM:okay


this.height = h;
this.desc = d; LOW:too easy
}

public String getDesc() { return desc; }


}

public class Main{


public static void main (String[] args) {
Level[] levels = Level.values();

for(Level l : levels)
System.out.println(l + ":" +l.getDesc());
}
}
-C#: Hey, Can I Copy Your Homework?
-Java: “Yeah, change it up a bit, so it looks like you didn’t copy.
C#: Sure thing Good Luck!
Q1.

enum AnimalTypes{ MAMMAL, BIRD, REPTILE, FISH}

public class Animal {


private String name;
private AnimalTypes type;

public Animal(String name, AnimalTypes type) {


this.name = name;
this.type = type;
}

public String toString(){


return name +" "+ type;
}
}
public class Zoo {
public Animal[] animals;
public Zoo(Animal[] animals) {
this.animals = animals;
}

public void printAnimals(){


for(Animal a: animals)
System.out.println(a);
}
}

public class MainClassQ1 {


public static void main(String[] args) {

Animal a1 = new Animal("eagle", AnimalTypes.BIRD);


Animal a2 = new Animal("salmon", AnimalTypes.FISH);
Animal a3 = new Animal("bear", AnimalTypes.MAMMAL);
Animal a4 = new Animal("snake", AnimalTypes.REPTILE);

Animal[] animals = {a1, a2, a3, a4};


Zoo zoo = new Zoo(animals);
zoo.printAnimals();
}
}
Q2.

public class Student {


private int midTerm;
private int finalExam;

public Student(int midTerm, int finalExam) {


setMidTerm(midTerm);
setFinalExam(finalExam);
}

public void setMidTerm(int midTerm) {


if (midTerm <0 || midTerm >100)
throw new IllegalArgumentException("midterm cannot be less than 0 or greater than 100");

this.midTerm = midTerm;
}

public void setFinalExam(int finalExam) {


if (finalExam <0 || finalExam >100)
throw new IllegalArgumentException("finalexam cannot be less than 0 or greater than 100");

this.finalExam = finalExam;
}

public int getMidTerm() {


return midTerm;
}

public int getFinalExam() {


return finalExam;
}

public double getAverage(){


return midTerm*0.4 + finalExam*0.6;
}
}

public class MainClassQ2 {

public static void main(String[] args){

try{
Student s = new Student(-5, 99);
System.out.println(s.getAverage());
}

catch(Exception e){
System.out.println(e.getMessage());
}
}
}

You might also like