ITS 1033_Take Home Assignemnt 01
ITS 1033_Take Home Assignemnt 01
02. Discuss the concepts of Reference Variables and Primitive Variables, providing suitable
examples for each.
03. Explain the concepts of Method Parameters, Local Variables, Default Values, and Declaration
Values with suitable examples.
06. Explain the difference between “instance variables” and “static variables” in Java using real-
world examples.
07. Which of the following lines are illegal? Explain your answer.
class Product {
private String name;
private double price;
public void setName(String n) {
name = n;
}
public void setPrice(double p) {
price = p;
}
}
class Main {
4 | Object Oriented Programming – Assignment 01
//--------------------Main.java------------------------
class Main {
public static void main(String args[]) {
5 | Object Oriented Programming – Assignment 01
//--------------------Main.java------------------------
class Main {
public static void main(String args[]) {
Person p1 = new Person("Alice", 30);
p1.printDetails();
}
}
11. Explain the purpose of constructors in a Java class with suitable examples.
}
public void printBalance() {
System.out.println("Account Number: " + accountNumber + ", Balance: " + balance);
}
}
//--------------------Main.java------------------------
class Main {
public static void main(String args[]) {
BankAccount acc1 = new BankAccount("12345", 1000.0);
acc1.deposit(500.0);
acc1.withdraw(200.0);
acc1.printBalance();
}
}
class Main {
public static void main(String args[]) {
Shape s1 = new Shape();
s1.printColor();
Shape s2 = new Shape("blue");
s2.printColor();
}
}
}
14. Which of the following lines of code could be inserted at line 12 and still allow the code to
compile.
class Student {
String name;
int age;
}
7 | Object Oriented Programming – Assignment 01
class Main {
public static void main(String args[]) {
// Insert code here
}
}
A. Student s1;
B. Student s1 = new Student();
C. new Student();
D. Student s1 = new Student("John", 21);
E. Student s1 = new Student(18);
class Main {
public static void main(String args[]) {
Animal a1 = new Animal();
System.out.print(a1.name); // Line 1
System.out.print(a1.age); // Line 2
}
}
class Main {
public static void main(String args[]) {
Test t1 = new Test(1, 2);
System.out.print(t1.a + " " + t1.b + " ");
Test t2 = new Test();
System.out.println(t2.a + " " + t2.b);
}
}
A. 3 4 3 4
B. 1 2 3 4
C. 3 4 0 0
D. 1 2 0 0
17. Which of the following lines of code could be inserted at line 10 to get the following output:
"Values 100 200"?
8 | Object Oriented Programming – Assignment 01
class MyClass {
/* Insert Code Here Line 10 */
void printValues() {
System.out.println("Values : " + x + " " + y);
}
}
class Main {
public static void main(String args[]) {
MyClass c = new MyClass(100, 200);
c.printValues();
}
}
A. int x; int y; MyClass(int i, int j) { x = i; y = j; }
B. int x = 0; int y = 0; MyClass(int i, int j) { x = i; y = j; }
C. int x; int y; MyClass() { x = 100; y = 200; }
D. int x = 0; int y = 0; MyClass() { x = 100; y = 200; }
18. Write a Java program to demonstrate the use of the "this" keyword.
19. What is difference between “Tightly encapsulated” and “Loosely encapsulated”? Explain your
answer with appropriate examples.
20. Create fully encapsulated class “Date” with the following functionalities.
//--------------------Date.java------------------------
class Date{
int year=1970;
int month=1;
int day=1;
}
//--------------------Demo.java------------------------
class Demo{
public static void main(String args[]){
Date d1=new Date();
d1.printDate(); //1970-1-1
d1.year=2016; //Illegal
d1.month=5; //Illegal
d1.day=30; //Illegal
/*year, month and day attributes
*cannot be accessed to another class
*/
d1.setYear(2016);
d1.setMonth(5);
d1.setDay(31);
9 | Object Oriented Programming – Assignment 01
System.out.println("Year : "+d1.getYear());
System.out.println("Month :"+d1.getMonth());
System.out.println("Day : "+d1.getDay()); }
}
// DemoEmployee.java
class DemoEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee("E001", "John Doe", 50000.0);
emp1.printEmployeeDetails();
emp1.printEmployeeDetails();
}
}
22. What will be the output when you compile and run the program?
class MyClass {
int a = 5;
static int b = 10;
System.out.println(obj1.a); // Line 1
System.out.println(obj1.b); // Line 2
System.out.println(obj2.a); // Line 3
System.out.println(obj2.b); // Line 4
}
}
A. 5, 10, 5, 10 B. 0, 10, 0, 10
C. 5, 0, 5, 0 D. 10, 5, 10, 5
23. What will be the output when you compile and run the program?
class MyClass {
int a;
static int b;
static {
b = 100; // Static initialization block
}
MyClass(int a) {
this.a = a;
11 | Object Oriented Programming – Assignment 01
void printValues() {
System.out.println(a + " " + MyClass.b);
}
obj1.setValues(5, 50);
obj2.setValues(15, 150);
obj1.printValues();
obj2.printValues();
obj1.printValues();
obj2.printValues();
}
}
A. Constructor called with a = 1, Constructor called with a = 10, 5 50, 15 150, 5 200, 15 200
B. Constructor called with a = 1, Constructor called with a = 10, 5 50, 15 150, 5 50, 15 50
C. Constructor called with a = 1, Constructor called with a = 10, 1 100, 10 100, 1 200, 10 200
D. Constructor called with a = 1, Constructor called with a = 10, 5 50, 15 150, 5 150, 15 150
24. Create a class Circle with attributes radius (default to 1.0) and color (default to "red"). Provide
methods that calculate the circle's area and circumference. It should include set and get methods
for both radius and color. The set method for radius should verify that the radius is a floating-
point number larger than 0.0 and less than 100.0. Write a program to test class Circle.
class Test{
int x=10;
static int y=20;
static void staticMethod(){}
void instanceMethod(){}
void mA(){
System.out.println(x); //Line 1
System.out.println(y); //Line 2
staticMethod(); //Line 3
instanceMethod(); //Line 4
}
static void mB(){
System.out.println(x); //Line 5
System.out.println(y); //Line 6
staticMethod(); //Line 7
instanceMethod(); //Line 8
}
}
26. Given:
class Sample {
private int a = 10;
static int b = 20;
System.out.println(obj.a); // Line 1
System.out.println(b); // Line 2
System.out.println(c); // Line 3
System.out.println(Sample.a); // Line 4
System.out.println(Sample.b); // Line 5
System.out.println(Sample.c); // Line 6
System.out.println(obj.b); // Line 7
System.out.println(obj.c); // Line 8
}
}
27. Create a class called Product that represents a product sold at a grocery store. A Product should
include four pieces of information as instance variables: a product code (type String), a product
name (type String), a quantity of the product in stock (type int), and a price per unit (type
double). Implement a constructor that initializes these instance variables.Provide set and get
methods for each instance variable. Ensure that the set methods validate that the quantity and
price per unit are positive values; if not, they should be set to default values (0 for quantity and
0.0 for price per unit).Additionally, include a method named calculateInventoryValue that
calculates the total value of the product inventory (quantity multiplied by price per unit) and
returns it as a double value.
Write a test application named ProductTest that demonstrates the Product class's capabilities by
creating instances of Product, setting values, and calculating the inventory value for each
product.
28. Create a class called BankAccount that represents a bank account. The BankAccount class
should have the following instance variables: an account number (type String), an account
holder's name (type String), the balance in the account (type double), and a boolean flag
indicating whether the account is active or not (boolean). Implement a constructor that
initializes these instance variables.
Provide set and get methods for each instance variable. Ensure that the set methods validate the
input data: the account number should be a non-empty string, the account holder's name should
be non-empty, and the balance should be a non-negative value. If invalid data is provided during
object creation, set default values as appropriate.
Include methods for depositing funds (deposit), withdrawing funds (withdraw), and transferring
funds (transfer) between two BankAccount objects. Ensure that withdrawal and transfer
operations are only permitted if the account is active (isActive is true) and if the balance is
sufficient.
Write a test application named BankAccountTest that demonstrates the BankAccount class's
capabilities. Create instances of BankAccount, perform deposit, withdrawal, and transfer
operations, and display the account details after each operation.
14 | Object Oriented Programming – Assignment 01