Create a class Car and instantiate it 3 times and display all 3 car information .
Code :
Car.java
/** * Created by PAPAN on 2/2/2017. * * Create a class Car and instantiate it 3 times and display all 3 car information */ class Car { String name; String color; double maxSpeed; double length; double weight; public void start(){ System.out.println(name+" started !!!"); } public void move(){ System.out.println(name+" moving !!!"); } public void stop(){ System.out.println(name+" stopped !!!"); } public void carDetails(){ System.out.println("\n --- Car Details --- \n\tName : "+name+"\n\tColor : "+color+"\n\tLength " + ": "+length+" cms"+"\n\tWeight : "+weight+" lbs"+ "\n\tMax Speed : "+maxSpeed+" kpm\n"); } }
CarTest.java
/** * Created by PAPAN on 2/2/2017. */ class CarTest { public static void main(String[] papan){ Car car1=new Car(); car1.name="Audi R8"; car1.color="Red"; car1.weight=1450; car1.length=175; car1.maxSpeed=450; car1.carDetails(); car1.start(); car1.move(); car1.stop(); Car car2=new Car(); car2.name="BMW i8"; car2.color="Blue"; car2.weight=1550; car2.length=185; car2.maxSpeed=350; car2.carDetails(); car2.start(); car2.move(); car2.stop(); Car car3=new Car(); car3.name="Mclaren P1"; car3.color="Black"; car3.weight=1670; car3.length=145; car3.maxSpeed=550; car3.carDetails(); car3.start(); car3.move(); car3.stop(); } }
Output :
— Car Details —
Name : Audi R8
Color : Red
Length : 175.0 cms
Weight : 1450.0 lbs
Max Speed : 450.0 kpm
Audi R8 started !!!
Audi R8 moving !!!
Audi R8 stopped !!!
— Car Details —
Name : BMW i8
Color : Blue
Length : 185.0 cms
Weight : 1550.0 lbs
Max Speed : 350.0 kpm
BMW i8 started !!!
BMW i8 moving !!!
BMW i8 stopped !!!
— Car Details —
Name : Mclaren P1
Color : Black
Length : 145.0 cms
Weight : 1670.0 lbs
Max Speed : 550.0 kpm
Mclaren P1 started !!!
Mclaren P1 moving !!!
Mclaren P1 stopped !!!
Create Employee class with at least 5 data member and 1 behavior and 3 different overloaded constructor., create 3 objects using different constructor to store 3 different employee details and display all the employees details.
Code :
Employee.java
/** * Created by PAPAN on 2/1/2017. * * Create Employee class with at least 5 data member and * 1 behaviour and 3 different overloaded constructor., * create 3 objects using different constructor to store * 3 different employee details and display all the employees details. * */ class Employee { int id; int departmentID; int mobileNo; double salary; String name; String emailID; public Employee(){ this.id=-1; this.departmentID=-1; this.name="Not specified"; this.mobileNo=-1; this.salary=-1; this.emailID="Not specified"; } public Employee(int id,int departmentID,String name){ this(); this.id=id; this.departmentID=departmentID; this.name=name; } public Employee(int id,int departmentID,String name,double salary,int mobileNo,String emailID){ this(id,departmentID,name); this.salary=salary; this.mobileNo=mobileNo; this.emailID=emailID; } public void doSomething(){ if(id!=-1) System.out.println(name+" is doing something..."); } public void showEmployeeDetails(){ System.out.println("\nEmoployee Details :\n\tName : "+name+"\n\tEmployee ID : "+id+"\n\tDepartment ID : "+departmentID); System.out.println("Contact Details : \n\tMoblie No : "+mobileNo+"\n\tEmail ID : "+emailID); System.out.println("Salary : Rs "+salary); } }
EmployeeTest.java
/** * Created by PAPAN on 2/1/2017. */ class EmployeeTest { public static void main(String[] papan){ Employee emp1=new Employee(110,10001,"Rahul Sharma", 45600,789456123,"rahul@gmail.com"); emp1.showEmployeeDetails(); emp1.doSomething(); Employee emp2=new Employee(120,10002,"Nishant Jha"); emp2.showEmployeeDetails(); emp2.doSomething(); Employee emp3=new Employee(); emp3.showEmployeeDetails(); emp3.doSomething(); } }
Output :
Emoployee Details :
Name : Rahul Sharma
Employee ID : 110
Department ID : 10001
Contact Details :
Moblie No : 789456123
Email ID : rahul@gmail.com
Salary : Rs 45600.0
Rahul Sharma is doing something…
Emoployee Details :
Name : Nishant Jha
Employee ID : 120
Department ID : 10002
Contact Details :
Moblie No : -1
Email ID : Not specified
Salary : Rs -1.0
Nishant Jha is doing something…
Emoployee Details :
Name : Not specified
Employee ID : -1
Department ID : -1
Contact Details :
Moblie No : -1
Email ID : Not specified
Salary : Rs -1.0