0% found this document useful (0 votes)
59 views

Subject Code: 222CCS Subject Name: Object Oriented Programming in JAVA Assignment - 1

The document contains questions and code snippets related to object-oriented programming concepts in Java. Question 1 contains true/false questions about OOP concepts like class methods, data encapsulation, and constructors. Question 2 contains a code snippet and asks to draw a diagram illustrating method calls and parameter passing. Question 3 provides a UML diagram for a Circle class and asks to write the corresponding Java code. Question 4 provides a UML diagram for a Car class with instructions to write the Java code, including overloading methods and using a Person class.

Uploaded by

MOoOSa ALasiri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Subject Code: 222CCS Subject Name: Object Oriented Programming in JAVA Assignment - 1

The document contains questions and code snippets related to object-oriented programming concepts in Java. Question 1 contains true/false questions about OOP concepts like class methods, data encapsulation, and constructors. Question 2 contains a code snippet and asks to draw a diagram illustrating method calls and parameter passing. Question 3 provides a UML diagram for a Circle class and asks to write the corresponding Java code. Question 4 provides a UML diagram for a Car class with instructions to write the Java code, including overloading methods and using a Person class.

Uploaded by

MOoOSa ALasiri
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Subject Code: 222CCS

Subject Name: Object Oriented Programming in JAVA


Assignment -1

Q1:
1- Public methods of a class determine the behavior of its instances. Internal details are
implemented by private methods and private data members.
a. True b. False

2- Declaring the data members private ensures the integrity of the class.
a. True b. False

3- If a data member is a constant, declare it as a class constant.


a. True b. False

4- Local variables and parameters are erased when the execution of a method is completed.
a. True b. False

5- No dot notation is necessary when you call a method from another method of the same
class
a. True b. False

6- A default constructor is a constructor that accepts no arguments and has no statements in its
body
a. True b. False

7- Once a programmer has added an explicitly defined constructor to a class, no default


constructor will be added to the class by the compiler.
a. True b. False

8- When we say “return an object from a method,” we are actually returning the address, or
reference, of an object to the caller.
a. True b. False

9- Optionally, dot notation with the reserved this can be used to refer to an object’s data
member from the object’s methods and constructors.
a. True b. False

10- Class methods can access only the class variables and the class constants of the class.
a. True b. False

11- Instance methods can call all other methods of the same class.
a. True b. False

12- Instance methods, including constructors, can access all types of data members.
a. True b. False
13- Class methods cannot call instance methods of the same class.
a. True b. False

14- If there is no constructor in a class

a) Compile faliure.
b) Runtime erorr.
c) Compiler automatically creates a default constructor.
d) Program terminate upnormally.

15. Which of the following constructors are invalid?

• public int ClassA(int one) {


...
}

• public ClassB(int one, int two) {


...
}

• void ClassC(){
...
}

Q2: What is the output from the following code? Draw an illustration to show the effect of calling
myMethod similar to figure 7.8 page 401?
class Question {
private int one;
public void myMethod( int one ) {
this.one = one;
one = 12;
}
}
class Test {
public static void main(String[] arg) {
int one = 30;
Question q = new Question();
q.myMethod(one);
System.out.println(one);
}
}
Q3: Consider the below UML diagram representing the class: “Circle”. Write the java code for the same.

Circle
-radius: double
-color : String
+Circle()
+Circle(color:String, radius:double)
+setRadius(radius:double):void
+getRadius(): double
+setColor(color:String):void
+getColor():String
+setRadiusColor(color:String, radius:double):void
+equals(c:Circle) : Boolean

The class Circle contains:

• 2 data members :
i. radius which is a double
ii. color which is a String
• two get methods or accessors (getRadius and getColor)
• three set methods or mutators (setRadius, setColor and setRadiusColor)
• the method equals which compares a Circle with another one by comparing radius and color of
each one.
• Two constructors :
i. Circle ( ) initializes the radius to 1 and color to “red”
ii. Circle (color: String, radius: double). You can use the method setRadiusColor in this
constructor.
• Add another class which contains the main method as follows:
o Define two objects obj1 and obj2 on Circle.
o Set the circle attributes for the first object by using the constructor and the second object
by using setRadiusColor method
o Compare both objects by using equals method
Q4: Write the Java code corresponding to the following UML diagram. You can read the instructions
below to understand more about the methods

Car

+ MAX_Weight: double
- counter: int
- id: int
- weight: double
- mileage: long
- owner : Person
+ Car ( weight : double, mileage : long, owner : Person )
+ convertMeterIntoMile ( meter : double ): double
+ convertMileIntoMeter ( mile : double ): double
+ computeFuelEconomy():double
+ computeFuelEconomy( consumptionRate: double):double
+ computeFuelEconomy( consumptionRate: double, errorRate: double):
double
+ setWeight(weight:double):void
+ getWeight():double
+ setMileage( mileage: long ): void
+ getMileage():long

Instructions:

1. Write a java code for the above class diagram for “Car”.
2. MAX_Weight must be a public class constant which is set to 6.4
3. The data member “counter” must be a class member and initialized to zero
4. Use “counter” to assign a unique id for each class instance ( counter++ ) by the constructor
5. The constructor is used to initialize all the instance variables by receiving the values from the
user. Don’t change the parameter’s names.
6. “convertMeterIntoMile” and “convertMileIntoMeter” are class methods:
a. convertMeterIntoMile: convert the reading into miles by dividing the receiving value
by 1.62
b. convertMileIntoMeter: convert the reading into meter by multiplying the receiving
value by 1.62
7. The method “computeFuelEconomy” must be overloaded and return a dummy calculation
8. In the main method:
a. Create an array of objects to hold five objects of class Car.
b. Add them to the array. Note that the code for class Person is included but you need to
import it from package “people”
c. By using for-loop, check if any of the objects exceeds MAX_Weight or not, and print
“heavy car” or light char”, accordingly.
d. Use both class methods to convert 100 km into miles and 200 miles into kilometers.
e. Using the first object in the array, call all of the computeFuelEconomy methods.
9. Add your code to a package, called “assignment1”
10. Include a clear screenshot of your code and the output.

package people;

public class Person{


private String name;
private int age;

public Person() {
name = "no name";
age = -1;
}

public Person(String name, int age) {


this.name = name;
this.age = age;
}

public String getName() {


return name;
}

public void setName(String n) {

name = n;
}

public void setAge(int a) {


age = a;
}

public int getAge() {


return age;
}

You might also like