0% found this document useful (0 votes)
31 views20 pages

Assignment 3

The document outlines a series of Java programming assignments focused on object-oriented concepts such as class creation, constructors, inheritance, and method overriding. Each assignment includes the definition of classes like Person, Student, Book, Vehicle, and others, demonstrating various programming principles including constructor overloading and multilevel inheritance. The main method in each class creates instances and displays their properties, showcasing the functionality of the implemented classes.

Uploaded by

Shafiqha Khanum
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)
31 views20 pages

Assignment 3

The document outlines a series of Java programming assignments focused on object-oriented concepts such as class creation, constructors, inheritance, and method overriding. Each assignment includes the definition of classes like Person, Student, Book, Vehicle, and others, demonstrating various programming principles including constructor overloading and multilevel inheritance. The main method in each class creates instances and displays their properties, showcasing the functionality of the implemented classes.

Uploaded by

Shafiqha Khanum
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

1.

Create a class Person with a constructor that initializes name and age
package Assignment3;
class Person {
String name;
int age;
Person(String name, int age) {
[Link] = name;
[Link] = age;
}
void display() {
[Link]("Name: " + name + "\n Age: " + age);
}
}
public class First{
public static void main(String[] args) {
Person p = new Person("kk", 30);
[Link]();
}
}
Output:

2. Create a class called Student that has two properties: name and rollNumber.
Write a parameterized constructor to initialize these values. Also, create a
method to display the student details. In the main method, create one object of
Student and display the details.

package Assignment3;
class Student {
String name;
int rollNumber;
Student(String name, int rollNumber) {
[Link] = name;
[Link] = rollNumber;
}
void display() {
[Link]("Name: " + name + "\nRoll Number: " + rollNumber);
}
}
public class Second {
public static void main(String[] args) {
Student s = new Student("olol",5);
[Link]();
}
}
Output:

3. Create a class Book with default and parameterized constructors.


package Assignment3;
class Book {
String title;
String author;
Book() {
title = "Unknown";
author = "Uknown";
}
Book(String title, String author) {
[Link] = title;
[Link] = author;
}
void display() {
[Link]("Title: " + title + "\nAuthor: " + author);
}
}
public class Third {
public static void main(String[] args) {
Book b1 = new Book();
[Link]();
Book b2 = new Book("Punishment and crime", "Foyodor");
[Link](); }
}
Output:
4. Demonstrate constructor overloading in a class Rectangle
package Assignment3;
class Rectangle {
int length;
int breadth;
Rectangle() {
length = 0;
breadth = 0;
}
Rectangle(int side) {
length = side;
breadth = side;
}
Rectangle(int length, int breadth) {
[Link] = length;
[Link] = breadth;
}
void display() {
[Link]("Length: " + length + "\nBreadth: " + breadth);
}
}
public class Fourth {
public static void main(String[] args) {
Rectangle r1 = new Rectangle();
[Link]();
Rectangle r2 = new Rectangle(5);
[Link]();
Rectangle r3 = new Rectangle(4, 8);
[Link]();
}
}

Output:

5. Create a base class Vehicle and a subclass Car with extra attributes
package Assignment3;
class Vehicle {
String brand;
int year;
Vehicle(String brand, int year) {
[Link] = brand;
[Link] = year;
}
void display() {
[Link]("Brand: " + brand + "\n Year: " + year);
}
}
class Car extends Vehicle {
String model;
int doors;
Car(String brand, int year, String model, int doors) {
super(brand, year);
[Link] = model;
[Link] = doors;
}
void display() {
[Link]();
[Link]("Model: " + model + "\nDoors: " + doors);
}
}
public class Fifyh {
public static void main(String[] args) {
Car c = new Car("Toyota", 2020, "Corolla", 4);
[Link]();
}
}
Output:

6. Create a class hierarchy with Employee -> Manager, using inheritance and
constructor chaining
package Assignment3;
class Employee {
String name;
int id;
Employee(String name, int id) {
[Link] = name;
[Link] = id;
}
void display() {
[Link]("Name: " + name + "\nID: " + id);
}
}
class Manager extends Employee {
String department;
Manager(String name, int id, String department) {
super(name, id);
[Link] = department;
}
void display() {
[Link]();
[Link]("Department: " + department);
}
}
public class Six {
public static void main(String[] args) {
Manager m = new Manager("kinn", 1, "Gamer");
[Link]();
}
}
Output:

7. Demonstrate multilevel inheritance with constructors


(Shape→PolygonTriangle)Use constructor overloading
package Assignment3;
class Shape {
String color;
Shape() {
color = "undefined";
}
Shape(String color) {
[Link] = color;
}
void display() {
[Link]("Color: " + color);
}
}
class Polygon extends Shape {
int sides;
Polygon() {
super();
sides = 0;
}

Polygon(String color, int sides) {


super(color);
[Link] = sides;
}
void display() {
[Link]();
[Link]("Sides: " + sides);
}
}
class Triangle extends Polygon {
double base;
double height;
Triangle() {
super();
base = 0;
height = 0;
}
Triangle(String color, int sides, double base, double height) {
super(color, sides);
[Link] = base;
[Link] = height;
}
void display() {
[Link]();
[Link]("Base: " + base + ",\nHeight: " + height);
}
}
public class Seventh {

public static void main(String[] args) {


Triangle t1 = new Triangle();
[Link]();
[Link]();
Triangle t2 = new Triangle("Blue", 3, 5.0, 4.0);
[Link]();

}
Output:

8. Use constructor overloading and inheritance with a BankAccount


and Savings Account
package Assignment3;

class BankAccount {
String accountHolder;

int accountNumber;

double balance;

BankAccount() {

accountHolder = "Unknown";

accountNumber = 0;

balance = 0.0;

BankAccount(String accountHolder, int accountNumber, double balance) {


[Link] = accountHolder;

[Link] = accountNumber;
[Link] = balance;

void display() {

[Link]("Account Holder: " + accountHolder);

[Link]("Account Number: " + accountNumber);

[Link]("Balance: $" + balance);

class SavingsAccount extends BankAccount {


double interestRate;

SavingsAccount() {
super();

interestRate = 0.0;

SavingsAccount(String accountHolder, int accountNumber, double balance, double interestRate)


{
super(accountHolder, accountNumber, balance);

void display() {

[Link]();

[Link]("Interest Rate: " + interestRate + "%");

public class Eighth {

public static void main(String[] args) {

SavingsAccount acc1 = new SavingsAccount();

[Link]();

[Link]();

SavingsAccount acc2 = new SavingsAccount("P=numan", 9956, 15000.0, 3.5);


[Link]();

Output:

9. Build a class hierarchy with abstract class Appliance, subclass Washing


Machine, and constructor logic
package Assignment3;

abstract class Appliance {

String brand;

String model;

public Appliance(String brand, String model) {

[Link] = brand;

[Link] = model;

}
public abstract void operate();

class WashingMachine extends Appliance {

int loadCapacity;

public WashingMachine(String brand, String model, int loadCapacity) {

super(brand, model);

[Link] = loadCapacity;

public void operate() {


[Link]("The " + brand + " " + model + " washing machine with " + loadCapacity +
"kg load capacity is now operating.");
}

public class Ninth {

public static void main(String[] args) {

WashingMachine wm = new WashingMachine("Samsung", "EcoBubble", 7);


[Link]();

}
Output:

10. Create a University system with multiple levels: Person Staff/Student,


demonstrate inheritance and constructor
package Assignment3;
class Person3 {

String name;

int age;

Person3(String name, int age) {

[Link] = name;

[Link] = age;

void display() {

[Link]("Name: " + name);

[Link]("Age: " + age);

}
}

class Student4 extends Person3 {

String studentId;

Student4(String name, int age, String studentId) {

super(name, age);

[Link] = studentId;

void display() {

[Link]();
[Link]("Student ID: " + studentId);

}
}

class Staff extends Person3 {

String staffId;

String department;

Staff(String name, int age, String staffId, String department) {


super(name, age);

[Link] = staffId;
[Link] = department;

void display() {

[Link]();

[Link]("Staff ID: " + staffId);

[Link]("Department: " + department);

public class Tenth {

public static void main(String[] args) {

Student4 s = new Student4("megha", 20, "S123");


Staff st = new Staff("snuli", 45, "T456", "Computer Science");

[Link]("Student Details:");

[Link]();

[Link]("\nStaff Details:");

[Link]();

Output:

You might also like