Nothing Special   »   [go: up one dir, main page]

Java Lab

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 20

Java Lab

1.a) Create class Person with attributes name, age and gender.

b) Create class Student with attribute grade and,

c) Create class Teacher with attribute salary.

Class Student and Teacher must inherit from Person.


In main class, create and print two students and one teacher.
Apply inheritance, setter and getter.

Code:

package person;

import java.util.*;

public class Person {

void pdetails(String name,int age,char gender)

System.out.println("Name "+name);

System.out.println("Age "+age);

System.out.println("Gender "+gender);

public static void main(String[] args) {

Teacher obj=new Teacher();

obj.pdetails("Ramu",34,'m');

obj.tdetails(503234,12000);

Student obj2=new Student();

obj2.pdetails("Raju",20,'m');
obj2.sdetails(1001,'S');

class Student extends Person

void sdetails(int id, char grade)

System.out.println("Student ID "+id+"\nGrade "+grade+"\n");

class Teacher extends Person

void tdetails(int id,float salary)

System.out.println("Teacher ID "+id+"\nSalary "+salary+"\n");

2. Create a new class Car with the following methods:

public void start()

public void stop()

public int drive(int howlong)


a) The method drive() has to return the total distance driven by the car for the
specified time. Use the following formula to calculate the distance:

Distance = howlong*60;

b) Write another class CarOwner and that creates an instance of the object Car
and call its methods. The result of each method call has to be printed using
System.out.println().

c) Create a subclass of Car named JamesBondCar and override the method


drive() there. Use the following formula to calculate the distance:

Distance = howlong*180;

d) Be creative, print some funny messages!!!

Code:

package car;

import java.util.*;

public class Car

int start,stop;

int howlong;

public void start(int start)

System.out.println("Your journey started in 24 format "+ start);

public void stop(int stop)


{

System.out.println("Your journey stoped "+ stop);

public void drive(int start,int stop)

int distance;

howlong=stop-start;

distance=howlong*60;

System.out.println("Your covered distance is "+ distance+"\n");

public static void main(String[] args) {

Car obj=new Car();

obj.start(9);

obj.stop(12);

obj.drive(9,12);

Carowner obj2=new Carowner();

obj2.start(4);

obj2.stop(6);

obj2.drive(4,6);

JamesBondCar obj3=new JamesBondCar();

obj3.drive(8,11);

}
}

class Carowner extends Car

class JamesBondCar extends Car

public void drive(int start,int stop)

int distance;

howlong=stop-start;

distance=howlong*180;

System.out.println("Your covered distance is "+ distance+"\n");

3. Create an Account class for a bank and write a main method in a different class
to briefly experiment with some instances of the Account class.

a) Using the Account class as a base class, write two derived classes called
SavingsAccount and CurrentAccount. A SavingsAccount object, in addition to the
attributes of an Accountobject, should have an interest variable and a method
which adds interest to the account. A CurrentAccount object, in addition to the
attributes of an Account object, should have an overdraft limit variable. Ensure that
you have overridden methods of the Account class as necessary in both derived
classes.

b) Now create a Bank class, an object of which contains an array of Account


objects. Accounts in the array could be instances of the Account class, the
SavingsAccount class, or theCurrentAccount class. Create some test accounts
(some of each type).

c) Write an update method in the bank class. It iterates through each account,
updating it in the following ways: Savings accounts get interest added (via the
method you already wrote); CurrentAccounts get a letter sent if they are in
overdraft.

d) The Bank class requires methods for opening and closing accounts, and for
paying a dividend into each account.

e) Note that the balance of an account may only be modified through the
deposit(double) and withdraw(double) methods.

Code:

public class Account {

private double balance;

private int acctNum;

public Account (int num)

balance = 0.0;

acctNum = num;

public void deposit (double amt)

{
if (amt >0)

balance +=amt;

else

System.out.println("Account.deposit(...): "+"cannot deposit negative


amount.");

public void withdraw (double amt)

if (amt>0)

balance -=amt;

else

System.err.println("Account.withdraw(...): " +"cannot withdraw negative


amount.");

public double getBalance()

return balance;

public double getAccountNumber()

return acctNum;

}
public String toString()

return "Acc " + acctNum + ": " + "balance = "+ balance;

public final void print()

System.out.println( toString());

public class SavingsAccount extends Account {

private double interest;

public SavingsAccount(int acctNum, double interest) {

super(acctNum);

this.interest=interest;

public double getInterest() {

double x= getBalance() + getBalance()*interest;

return x;

public void AddInterest (double interest) {

double x = super.getBalance() * interest;

super.deposit(x);

}
public String toString() {

return super.toString()+" Interest : " + interest;

public class CheckingAccount extends Account {

private double limit;

public CheckingAccount(int acctNum, double limit) {

super(acctNum);

this.limit=limit;

public double getLimit() {

return this.limit;

public void setLimit(double limit) {

this.limit=limit;

public void withdraw (double limit) {

if (limit <= this.limit)

super.withdraw(limit);

else {
System.out.println(" Sorry, Limit Exceeded" );

public String toString() {

return super.toString() +"Limit : "+limit;

public class Bank {

public static void main(String[] args) {

Account[] accounts = new Account[2];

accounts[0] = new SavingsAccount(2, 0.25);

accounts[1] = new CheckingAccount(23, 50);

for(int i=0; i<accounts.length;i++) {

if (accounts[0].equals(SavingsAccount)

System.out.println(accounts[0].getInterest());

4.Write an abstract class with an abstract method double Process (double P, double
R). Create a subclass Discount and implement the Process() method with the
following formula: net=P-P*R/100. Return the net value. Create another subclass
Tax and implement the Process() method with the following formula:
total=P+P*R/100. Return the total.
Code:

package abstract;

public class Abstract {

double Process(double P, double R)

System.out.println(P+R);

return 0;

public static void main(String[] args) {

Discount obj1=new Discount();

obj1.Process(23,56);

Tax obj2=new Tax();

obj2.Process(23,32);

Abstract obj=new Abstract();

obj.Process(54, 45);

class Discount extends Abstract

double Process(double P,double R)

double net;
net=P-((P*R)/100);

System.out.println("the net value is"+net);

return 0;

class Tax extends Abstract

double Process(double P,double R)

double total;

total=P+((P+R)/100);

System.out.println("Total is"+total);

return 0;

5. Write a program to create three classes Person, Address and Student. Student
class is inherited from Person class. Address is a member of Person class. Write
suitable constructors. Write a method display to display the student/person
details. Also include a method in student class to list out all the students residing in
a particular city.

a) Person Name, Age, Gender, Height, Weight, Address


b) Method Display()

c) Student Regno, Course

d) Methods Display() , Display(city)

Code:

public class Person extends Test

String name;

int age;

Person()

name = "";

age = 0;

address = "";

Person(String name, int age, String address)

this.name = name;

this.age = age;

this.address = address;

String getName()

{
return name;

public void display()

System.out.println("Name = "+ name);

System.out.println("Age = "+ age);

System.out.println("address = "+ address);

public class Student extends Person

int studentNum, semester;

Student(String name, int age, String address, int studentNum, int semester)

super(name, age, address);

this.studentNum = studentNum;

this.semester = semester;

public String getName()

return name;

public void setName(String name)


{

this.name = name;

public int getAge() // age

return age;

public void setAge(int age)

this.age = age;

public String getAddress() // address

return address;

public void setAddress(String address)

this.address = address;

public int getStudentNum() // studentNum

return studentNum;
}

public void setStudentNum(int studentNum)

this.studentNum = studentNum;

public int getSemester()

return semester;

public void setSemester(int semester)

this.semester = semester;

void Display()

public class Course extends Student

String course;
Course(String name, int age, String address, int studentNum, int semester, String
course)

super(name, age, address, studentNum, semester);

this.course = course;

public void display()

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.util.ArrayList;

import java.util.List;

public class Test implements StudentFees

public static void main(String args[ ]) throws IOException

System.out.println("============" + "=================");

System.out.println("Students " + "Personal Details");

System.out.println("============" + "=================");
String name, address, course;

int age, studentNum, semester;

List<Student> studentsList = new ArrayList<Student>(); // array list to store


user input

for (int i = 0; i < 2; i++)

int studentNumber = (i + 1);

InputStreamReader converter = new InputStreamReader(System.in);

BufferedReader in = new BufferedReader(converter);

System.out.println("Enter Student "+ studentNumber + " Name:");

name = in.readLine();

System.out.println("Enter Student " + studentNumber + " Age (Integer):");

age = Integer.valueOf(in.readLine());

System.out.println("Enter Student " + studentNumber + " Address:");

address = in.readLine();

System.out.println("Enter Student " + studentNumber + " Number:");

studentNum = Integer.valueOf(in.readLine());

System.out.println("Enter Student " + studentNumber + " Semester:");

semester = Integer.valueOf(in.readLine());

System.out.println("Enter Student " + studentNumber + " Course:");

course = in.readLine();

Student student = new Student(name, age, address, studentNum,


studentNum);
studentsList.add(student

for (int j = 0; j < studentsList.size(); j++)

Student st = studentsList.get(j);

System.out.println("Information of Student : " + (j + 1));

System.out.println("");

System.out.println("Name: " + st.getName() + " - Age: "+st.getAge() + " -


Address: " + st.getAddress() + " - Student Number: " + st.getStudentNum() + " -
Semester: " + st.getSemester() + " - Course: " + st.getCourse()); // print out results
entered by user

System.out.println("");

String course;

public String getCourse()

return course;

public void setCourse(String course)

this.course = course;
}

public void payFees(float fees)

You might also like