Java Lab
Java Lab
1.a) Create class Person with attributes name, age and gender.
Code:
package person;
import java.util.*;
System.out.println("Name "+name);
System.out.println("Age "+age);
System.out.println("Gender "+gender);
obj.pdetails("Ramu",34,'m');
obj.tdetails(503234,12000);
obj2.pdetails("Raju",20,'m');
obj2.sdetails(1001,'S');
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().
Distance = howlong*180;
Code:
package car;
import java.util.*;
int start,stop;
int howlong;
int distance;
howlong=stop-start;
distance=howlong*60;
obj.start(9);
obj.stop(12);
obj.drive(9,12);
obj2.start(4);
obj2.stop(6);
obj2.drive(4,6);
obj3.drive(8,11);
}
}
int distance;
howlong=stop-start;
distance=howlong*180;
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.
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:
balance = 0.0;
acctNum = num;
{
if (amt >0)
balance +=amt;
else
if (amt>0)
balance -=amt;
else
return balance;
return acctNum;
}
public String toString()
System.out.println( toString());
super(acctNum);
this.interest=interest;
return x;
super.deposit(x);
}
public String toString() {
super(acctNum);
this.limit=limit;
return this.limit;
this.limit=limit;
super.withdraw(limit);
else {
System.out.println(" Sorry, Limit Exceeded" );
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;
System.out.println(P+R);
return 0;
obj1.Process(23,56);
obj2.Process(23,32);
obj.Process(54, 45);
double net;
net=P-((P*R)/100);
return 0;
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.
Code:
String name;
int age;
Person()
name = "";
age = 0;
address = "";
this.name = name;
this.age = age;
this.address = address;
String getName()
{
return name;
Student(String name, int age, String address, int studentNum, int semester)
this.studentNum = studentNum;
this.semester = semester;
return name;
this.name = name;
return age;
this.age = age;
return address;
this.address = address;
return studentNum;
}
this.studentNum = studentNum;
return semester;
this.semester = semester;
void Display()
String course;
Course(String name, int age, String address, int studentNum, int semester, String
course)
this.course = course;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
System.out.println("============" + "=================");
System.out.println("============" + "=================");
String name, address, course;
name = in.readLine();
age = Integer.valueOf(in.readLine());
address = in.readLine();
studentNum = Integer.valueOf(in.readLine());
semester = Integer.valueOf(in.readLine());
course = in.readLine();
Student st = studentsList.get(j);
System.out.println("");
System.out.println("");
String course;
return course;
this.course = course;
}