[go: up one dir, main page]

0% found this document useful (0 votes)
13 views14 pages

ITS 1033_Take Home Assignemnt 01

Uploaded by

yehara nethsilu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views14 pages

ITS 1033_Take Home Assignemnt 01

Uploaded by

yehara nethsilu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

1 | Object Oriented Programming – Assignment 01

Institute of Software Engineering


Graduate Diploma in Software Engineering (GDSE)

Batch – GDSE 71/72


Module – Object-Oriented Programming
Assignment – 01
2 | Object Oriented Programming – Assignment 01

01. Explain the concepts of Class/Template, Object/Instance, Attributes/Properties, and


Methods/Functions with suitable Real-world examples

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.

04. Given Code:


//--------------------Student.java------------------------
class Student {
private String name;
private int age;
public Student() {
name = "Unknown";
age = 0;
}
public void setName(String n) {
name = n;
}
public void setAge(int a) {
age = a;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
//--------------------Main.java------------------------
class Main {
public static void main(String args[]) {
Student student1 = new Student();
student1.setName("Alice");
student1.setAge(21);
System.out.println(student1.getName());
System.out.println(student1.getAge());
}
}

What is the result of attempting to compile and run the program?


A. Alice 21 B. Unknown 0
C. Alice 0 D. Compilation error due to private fields
E. None of the above
3 | Object Oriented Programming – Assignment 01

05. Given Code:


//--------------------Book.java------------------------
class Book {
private String title;
private String author;
public Book(String t, String a) {
title = t;
author = a;
}
public String getTitle() {
return title;
}
public String getAuthor() {
return author;
}
}
//--------------------Library.java------------------------
class Library {
public static void main(String args[]) {
Book book1 = new Book("1984", "George Orwell");
System.out.println(book1.getTitle());
System.out.println(book1.getAuthor());
}
}

What is the result of attempting to compile and run the program?


A. 1984 George Orwell
B. Compilation error due to private fields
C. Null Null
D. Compilation error due to missing methods
E. None of the above

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

public static void main(String args[]) {


Product p1 = new Product(); // Line 1
System.out.println("Product name: " + name); // Line 2
System.out.println("Product price: " + price); // Line 3
}
}

08. Analyze the following code and explain the output.


//--------------------Computer.java------------------------
class Computer {
private String brand;
private int ram;

public void setDetails(String b, int r) {


brand = b;
ram = r;
}
public void printDetails() {
System.out.println("Brand: " + brand + ", RAM: " + ram + "GB");
}
}
//--------------------Main.java------------------------
class Main {
public static void main(String args[]) {
Computer comp1 = new Computer();
comp1.setDetails("Dell", 16);
comp1.printDetails();
}
}

09. Analyze the following code and explain the output.


//--------------------Movie.java------------------------
class Movie {
private String title;
private double rating;
public Movie(String t, double r) {
title = t;
rating = r;
}
public void printDetails() {
System.out.println("Title: " + title + ", Rating: " + rating);
}
}

//--------------------Main.java------------------------
class Main {
public static void main(String args[]) {
5 | Object Oriented Programming – Assignment 01

Movie m1 = new Movie("Inception", 8.8);


m1.printDetails();
}
}

10. Analyze the following code and explain the output.


//--------------------Person.java------------------------
class Person {
private String name;
private int age;
public Person(String n, int a) {
name = n;
age = a;
}
public void printDetails() {
System.out.println("Name: " + name + ", Age: " + age);
}
}

//--------------------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.

12. Analyze the following code and explain the output.


//--------------------BankAccount.java------------------------
class BankAccount {
private String accountNumber;
private double balance;
public BankAccount(String accNum, double bal) {
accountNumber = accNum;
balance = bal;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if(balance >= amount) {
balance -= amount;
} else {
System.out.println("Insufficient funds");
}
6 | Object Oriented Programming – Assignment 01

}
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();
}
}

13. Analyze the following code and explain the output.


class Shape {
private String color;
public Shape() {
color = "red";
}
public Shape(String c) {
color = c;
}
public void printColor() {
System.out.println("Color: " + color);
}
}

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);

15. Analyze the following code and explain the output


class Animal {
String name;
int age;
}

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
}
}

16. Analyze the following code and explain the output.


class Test {
int a = 3;
int b = 4;
Test(int i, int j) { a = i; b = j; }
Test() {}
}

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()); }
}

21. Given Code,


// Employee.java
class Employee {
private String employeeId;
private String name;
private double salary;

public Employee(String employeeId, String name, double salary) {


this.employeeId = employeeId;
this.name = name;
this.salary = salary;
}

public void printEmployeeDetails() {


System.out.println("Employee ID: " + employeeId);
System.out.println("Name: " + name);
System.out.println("Salary: $" + salary);
}

public void setName(String name) {


this.name = name;
}

public void setSalary(double salary) {


this.salary = salary;
}
}

// DemoEmployee.java
class DemoEmployee {
public static void main(String[] args) {
Employee emp1 = new Employee("E001", "John Doe", 50000.0);
emp1.printEmployeeDetails();

Employee emp2 = new Employee("E002", "Jane Smith", 60000.0);


emp2.printEmployeeDetails();

// Update employee details


emp1.setName("John Johnson");
emp1.setSalary(55000.0);
10 | Object Oriented Programming – Assignment 01

emp1.printEmployeeDetails();
}
}

A. Explain how encapsulation is implemented in the Employee class. Provide examples


of private fields and public methods that demonstrate encapsulation.
B. Discuss the advantages of encapsulation in the Employee class. How does
encapsulation contribute to code maintainability and security?
C. Compare and contrast the use of constructors and setter methods in initializing and
updating employee details in the Employee class. What are the benefits of each approach?
D. Describe how method visibility modifiers (private, public) are utilized in the
Employee class. Provide examples of methods with different visibility and explain their
significance.
E. Explain the purpose of the DemoEmployee class. How does it demonstrate the
instantiation and utilization of Employee objects?

22. What will be the output when you compile and run the program?
class MyClass {
int a = 5;
static int b = 10;

public static void main(String[] args) {


MyClass obj1 = new MyClass();
MyClass obj2 = new MyClass();

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

System.out.println("Constructor called with a = " + a);


}

void setValues(int a, int b) {


this.a = a;
MyClass.b = b;
}

void printValues() {
System.out.println(a + " " + MyClass.b);
}

static void changeStaticValue(int newValue) {


b = newValue;
}
}

public class DemoMyClass {


public static void main(String[] args) {
MyClass obj1 = new MyClass(1);
MyClass obj2 = new MyClass(10);

obj1.setValues(5, 50);
obj2.setValues(15, 150);

obj1.printValues();
obj2.printValues();

MyClass.changeStaticValue(200); // Changing static variable through a static method

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.

25. Which of the following code lines are illegal?


12 | Object Oriented Programming – Assignment 01

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;

public static void main(String[] args) {


int c = 30;
Sample obj = new Sample();

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
}
}

Which of these lines will cause compile error?


A. Line 4 and Line 6
B. Line 5 and Line 7
C. Line 6 and Line 8
D. Line 1 and Line 8
13 | Object Oriented Programming – Assignment 01

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

You might also like