ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
Classes and Objects
You will learn how to write your own classes, including declaring member data fields, methods, and
constructors. You will learn to use your classes to create objects and how to use the objects you create.
Solutions (Answers)
Part 1: Program Outputs
1. (Q #1 Answer)
a. Instance Variables: a, b, c, e and myArray1
Class/Static Variables: d and myArray2
b. Local Variables: msg1, msg2 and x.
c. Parameters: p1, p2, p3, y and args.
d. There are 4 methods:
- 2 Static methods: myMethodThree(int y), and main(String[] args)
- 2 instance methods: myMethodOne(),and myMethodTwo()
e. There are 2 Constructors
- Example1()
- Example1(int p1, byte p2, double p3)
f. The output is:
This is just an Example
2. (Q #2 Answer)
0
11
22
22
22
3. (Q #3 Answer)
43
20
53
53
53
0
11
53
53
53
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 1
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
4. (Q #4 Answer)
43
44
55
53
54
55
66
66
66
66
5. (Q #5 Answer)
num1 = 33
num2 = 44
num1 = 33
num2 = 44
num1 = 78
num2 = 44
num1 = 89
num2 = 44
num1 = 78
num2 = 26
num1 = 89
num2 = 26
num1 = 78
num2 = 200
num1 = 89
num2 = 200
6. (Q #6 Answer)
33
44
123
456
7. (Q #7 Answer)
0
1
2
3
4
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 2
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
8. (Q #8 Answer)
1
2
3
99
2
3
33
44
55
Part 2: Programming/Coding
1. (Q #1 Answer)
a. public void myPrinter(String msg, int num) // or other answers
void myPrinter(String msg, int num) // or you can use other names
void printer(String x, int n)
b. public boolean myTesterr(int num) // or other answers
boolean myTester(int num) // or you can use other names
boolean isPositive(int n)
c. public int myAdder(int[] nums) // or other answers
int myAdder(int[] nums)
2. (Q #2 Answer)
public int getMax(int num1, int num2, int num3){
if(num1 > num2){
if(num1 > num3)
return num1;
else
return num3;
}
else if(num2 > num3)
return num2;
else
return num3;
}
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 3
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
3. (Q #3 Answer)
public double average(double num1, double num2, double num3){
double sum = num1 + num2 + num3;
double avg = sum/3;
return avg;
}
4. (Q #4 Answer)
public double myMethod(double n1, double n2, double n3){
double sum = n1 + n2;
return sum*n3;
}
5. (Q #5 Answer)
public boolean isPrime(int n){
if(n%2 == 0)
return false;
else
return true;
}
6. (Q #6 Answer)
public static int[] myMethod(String p1, double p2){
}
Note:
- Method name: myMethod
- It is visible outside the class, so we use access modifier public
- It can only access class (static) variables means it is static method, hence we use the modifier
static.
- It returns an array of integers means its return type is array of integer: int[ ]
- It accepts two parameters which are a String and double, in that order: String p1, double p2
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 4
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
7. (Q #7 Answer)
public class Rectangle {
//Two double data fields named width and height
double width;
double height;
//A no-arg constructor that creates a default rectangle
public Rectangle(){
this.width = 0.0;
this.height = 0.0;
}
//A constructor with the specified width and height
public Rectangle(double w, double h){
this.width = w;
this.height = h;
}
//A method named getArea() that returns the area
public double getArea(){
double area = width*height;
return area;
}
//A method named getPerimeter() that returns the perimeter
public double getPerimeter(){
double perimeter = 2*(width + height);
return perimeter;
}
}
8. (Q #8 Answer)
public class TestRectangle {
public static void main(String[] args) {
Rectangle recOne=new Rectangle(4,40);
Rectangle recTwo=new Rectangle(3.5, 35.9);
System.out.println("Width of Rectangle1: " + recOne.width);
System.out.println("Height of Rectangle1: " + recOne.height);
System.out.println("Area of Rectangle1: " + recOne.getArea());
System.out.println("Perimeter of Rectangle1: " + recOne.getPerimeter());
System.out.println("Width of Rectangle2: " + recTwo.width);
System.out.println("Height of Rectangle2: " + recTwo.height);
System.out.println("Area of Rectangle2: " + recTwo.getArea());
System.out.println("Perimeter of Rectangle2: " + recTwo.getPerimeter());
}
}
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 5
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
9. (Q #9 Answer)
public class Time {
int hour;
int minute;
int second;
public Time(){
this.hour=0;
this.minute=0;
this.second=0;
}
public Time(int h, int m, int s){
this.hour=h;
this.minute=m;
this.second=s;
}
public int getHour(){
return this.hour;
}
public int getMinute(){
return this.minute;
}
public int getSecond(){
return this.second;
}
public void setTime(int h, int m, int s){
this.hour=h;
this.minute=m;
this.second=s;
}
}
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 6
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
10. (Q #10 Answer)
public class Employee {
String firstName;
String lastName;
double monthlySalary;
public Employee(String fn, String ln, double s){
this.firstName=fn;
this.lastName=ln;
this.monthlySalary=s;
}
public void setFName(String x){
this.firstName=x;
}
public void setLName(String x){
this.lastName=x;
}
public void setSalary(double x){
this.monthlySalary=x;
}
public String getFName(){
return firstName;
}
public String getLName(){
return lastName;
}
public double getSalary(){
return monthlySalary;
}
}
11. (Q #11 Answer)
public class EmployeeTest{
public static void main(String[] args) {
Employee empOne = new Employee("Desta", "Hagos", 5000);
Employee empTwo = new Employee("Feyissa", "Ahmed", 6000);
double empOneYearlySalary = empOne.getSalary()*12;
double empTwoYearlySalary = empTwo.getSalary()*12;
System.out.println("Yearly salary of " + empOne.getFName() + " is "+ empOneYearlySalary );
System.out.println("Yearly salary of " + empTwo.getFName() + " is "+ empTwoYearlySalary );
}
}
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 7
ECEG – 3101: Object Oriented Programming (OOP)
(Programming Examples – 2: Solutions)
Prepared by: Bushra K/Mariam Email: bushra.kmb@dbu.edu.et
12. (Q #12 Answer)
public class Account {
private int id=0;
private double balance=0.0;
private static double annualInterestRate=0.0;
public Account(){
this.id=0;
this.balance=0.0;
}
public Account(int i, double b){
this.id=i;
this.balance=b;
}
public void deposit(double x){
balance+=x;
}
public void withdraw(double x){
balance-=x;
}
}
(Thank you)
DBU, College of Engineering, Dep’t. of ECE, 2015e.c 8