Java Assignment
Java Assignment
Java Assignment
1. Introduce class and object. Explain the major four features of object-oriented
programming?
Ans: Class is a blueprint or template for creating objects. It defines how objects will work in
the class which organizes code and reusable components. For example, consider a car as a
class which defines the attributes such as color, model, break, acceleration and turn methods.
An example to illustrate how class and objects are created and used is given below:
System.out.println("Hello World!");
myObj.display();
2.They used to break big complex problems 2.They used a methodology where you start to
into smaller steps of procedures. create object to build complex systems.
3.They emphasize the use of control 3.The emphasizes the use of class, and object,
structures like loops, conditional, and encapsulation, inheritance, and polymorphism.
subroutines.
4.They passed the data to function as 4.They accessed the data and modified it
arguments and return types. through methods.
5.Once the code is reused, it leads to less Reusability of code is achieved through
flexibility. inheritance and polymorphism.
6.They are easy to use, understand and 6.They are suitable for large and complex
maintainable for small programs. programs.
3. In what ways static data members are different from instance Data members?
Ans: Static data members are different from instance data members because static data members
belong to the class itself rather than a particular instance. They are shared across all instances of
the class. When objects of its class are created, they share the same copy of the static field. When
a class member is declared as static it can be accessed without creating any object of its class.
Ans: Encapsulation means wrapping a code and data together that operate on the data into a
single unit. It is achieved by using access modifiers (private, public, protected and default). They
are hidden from other classes and can only be accessed by the methods of the class in which they
are found. Encapsulation is an object-oriented procedure of combining data members and data
methods of the class inside the user defined class which is necessary to declare as private.
1. Encapsulation allows us to control the access to the data by making class variables
private and providing public getter and setter methods to access and update the values.
2. Encapsulation helps to hide the implementation details and expose only necessary
methods.
3. Encapsulation enhances the security of application by reducing the risk of data being
corrupted and unauthorized access which enhances the data remains constant and secure.
4. Encapsulation improves code maintainability, readability, and security.
5. Why do we make data members private? Explain the use of getter and setter
methods with examples.
Ans: We make data members private to ensure encapsulation. It helps in restricting direct access
to the data from outside the class, promoting data security and better control over how the data is
accessed and modified.
Here we create a class person with private instance variables: name, age by using getter and
setter methods.
return name;
this.name = name;
return age;
}
// Setter for 'age'
this.age = age;
} else {
person.setName("Alice");
person.setAge(30);
person.setAge(-5);
}
Explanation
1. Instance Variables:
These are declared as private to restrict direct access from outside the class.
2. Getter Methods:
3. Setter Methods:
o public void setName(String name): Sets the value of the name variable.
o public void setAge(int age): Sets the value of the age variable, with a validation
check to ensure age is not negative.
4. Main Method:
o The setName and setAge methods are used to set the values of name and age.
o The getName and getAge methods are used to retrieve and print the values of
name and age.
o An attempt to set a negative age demonstrates the validation logic in the setAge
method.
Ans: Constructor chaining is the chaining where one constructor calls another constructor in the
same class which helps to reduce redundancy and maintain a clean codebase. This process is
used when we want to perform multiple tasks rather than creating a code for each task in a
single constructor.
Here's an example demonstrating constructor chaining using the super keyword in Java. We'll
create two classes: Person (superclass) and Employee (subclass).
Superclass: Person
public class Person {
// Default constructor
public Person () {
// Parameterized constructor
this.name = name;
this.age = age;
// Getters
return name;
return age;
}
}
Subclass: Employee
// Default constructor
public Employee () {
// Parameterized constructor
this.department = department;
// Getter
return department;
Explanation:
Superclass: Person
● Parameterized Constructor: Initializes name and age attributes and prints a message
indicating it has been called.
Subclass: Employee
● Default Constructor: Calls the parameterized constructor with default values and prints
a message.
Main Method:
● Creates two Employee objects using the default and parameterized constructors. The
output shows the order in which constructors are called due to chaining:
o For employee1, it calls the Employee default constructor, which calls the
Employee parameterized constructor, which in turn calls the Person parameterized
constructor.
contains:
Student id
Student name
Answer:
}
}
contains:
Employee empId
Employee salary
Answer:
3. Write a program of Car and initializes it through Method that contains:
Car name
Car color
Car price
Answer:
4. Write a program of Rectangle and initializes it through Method that contains:
Rectangle length
Rectangle breadth
Answer:
Cuboid length
Cuboid breadth
Cuboid height
Answer:
6. Write a program of Circle and initializes it through Constructor that contains:
Circle radius
Answer:
7. Write a program of Account and initializes it through Constructor that contains:
Account name
Account amount
Answer:
this.accountName = accountName;
this.accountAmount = accountAmount;
accountAmount -= amount;
} else {
if (amount > 0) {
accountAmount += amount;
account.displayAccountInfo();
account.deposit(150.00);
account.displayAccountInfo();
account.withdraw(100.00);
account.displayAccountInfo();
account.withdraw(600.00);
account.displayAccountInfo();
Answer:
// Static variable
static int staticCounter = 0;
// Instance variable
int instanceCounter = 0;
// Constructor
public StaticExample() {
staticCounter++;
instanceCounter++;
Answer:
int var_1 = 0;
return ++var;
return ++var_1;
System.out.println(Example_2.method_1());
System.out.println(myObj.method_2());
}
Explanation: In this program, we can utilize static methods without the creation of an instance
since it belongs to the class itself. But to utilize the non-static method, we needed to create an
object.
Answer:
this.name = name;
return name;
myObj.setName(“Dipesh”);
System.out.println(myObj.getName());
11. Create Class Car and create four Constructor which has
default constructor
one parameterized constructor < name >
four objects of Car (c1, c2, c3, c4) that calls respective constructor
Answer:
// Default constructor
public Car() {
this.name = "Unknown";
this.color = "Unknown";
this.price = 0.0;
this.name = name;
this.color = "Unknown";
this.price = 0.0;
this.name = name;
this.color = color;
this.price = 0.0;
this.name = name;
this.color = color;
this.price = price;
c1.displayDetails();
System.out.println();
c2.displayDetails();
System.out.println();
c3.displayDetails();
System.out.println();
c4.displayDetails();