Lab2 Opp
Lab2 Opp
LAB # 01
1. Task # 01: Create a class student which contains the basic data about the student that takes the basic
student data and displays it by using display method. An option of update is being provided to the user if
he/she want to update the data, the required data being updated.
Solution:
MAIN CODE:
lab obj=new lab();
Scanner input=new Scanner(System.in);
obj.input();
obj.display();
boolean x=true;
while(x){
SHAHAB TAHIR 1
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
}
public void display(){
System.out.println( "your name is :"+name);
System.out.println( "your father name is :"+fathername);
System.out.println( "your Enrollement is :"+Enrollement);
System.out.println( "your nof is :"+nof);
System.out.println( "your age is :"+age);
}
public void update(int i){
switch(i){
case 1: {
System.out.println("Enter your name :");
name = input.next();
break;
}
case 2:
{
System.out.println( "Enter your father name :");
fathername=input.next();
break;}
case 3:{
System.out.println( "Enter your Enrollement :");
Enrollement=input.next();
break;}
SHAHAB TAHIR 2
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
case 4:{
System.out.println( "Enter your nof :");
nof=input.nextInt();
break;}
case 5:{
System.out.println( "Enter your age :");
age=input.nextInt();
break;}
default:{
System.out.println( "good bye");
break;}
}
}
}
Output:
SHAHAB TAHIR 3
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
LAB TASK#2: . Create a class “computer” which contains specifications of computer, the program
shall ask the user does he/she wants to open the system, if the user press “yes” then the system starts
shows the initial loading and then displays the basic configuration of a system (by calling the method of
display () ), update option is being provided by the user, values of the specified items are being updated
once user decides to update that item.
SOLUTION:
MAIN CODE:
lab obj=new lab();
Scanner input=new Scanner(System.in);
obj.input();
obj.display();
boolean x=true;
while(x){
SHAHAB TAHIR 4
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
choice= input.nextInt();
if (choice==1) {
System.out.println("What do you want to update\n 1.windows name\n 2.system name\n
3.RAM\n 4.Hard Disks(gb)\n 5.SSD(gb)");
int i = input.nextInt();
obj.update(i);
}
else {
x=false;
obj.display();
}
NEW CLASS:
String windows;
String system;
String RAM;
String Hardisk;
String SSD;
Scanner input=new Scanner(System.in);
public void input(){
SHAHAB TAHIR 5
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
}
public void display(){
System.out.println( "your windows name is :"+windows);
System.out.println( "your system name is :"+system);
System.out.println( "your RAM is :"+RAM);
System.out.println( "your Hard disk(gb) is :"+Hardisk);
System.out.println( "your SSD(gb) is :"+SSD);
}
public void update(int i){
switch(i){
case 1: {
System.out.println("Enter your windows name :");
windows = input.next();
break;
}
case 2:
{
System.out.println( "Enter your system name :");
system=input.next();
break;}
case 3:{
System.out.println( "Enter your RAM :");
RAM=input.next();
break;}
case 4:{
SHAHAB TAHIR 6
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
break;}
}
OUTPUT:
SHAHAB TAHIR 7
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
SHAHAB TAHIR 8
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
TASK NO#3: Create a class of Employee which contains basic information about an employee,
employee name, father’s name and salary etc are being displayed by the display method and the salary of
employees or the designation of the employees are being set/updated as per need.
SOLUTON:
Main class:
TALHA obj=new TALHA();
Scanner input=new Scanner(System.in);
obj.input();
obj.display();
boolean x=true;
while(x){
SHAHAB TAHIR 9
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
int i = input.nextInt();
obj.update(i);
}
else {
x=false;
obj.display();
}
NEW CLASS CODE:
package com.company;
import java.util.Scanner;
public class lab {
String Employeename;
String fathername;
int salary;
String designation;
SHAHAB TAHIR 10
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
}
public void display(){
System.out.println( "your Employee name is :"+Employeename);
System.out.println( "your father name is :"+fathername);
System.out.println( "your salary is :"+salary);
System.out.println( "your designation is :"+designation);
}
public void update(int i){
switch(i){
case 1: {
System.out.println("Enter your Employee name :");
Employeename = input.next();
break;
}
case 2:
{
System.out.println( "Enter your father name :");
fathername=input.next();
break;}
case 3:{
System.out.println( "Enter your salary :");
salary=input.nextInt();
break;}
case 4:{
System.out.println( "Enter your designation :");
designation=input.next();
break;
SHAHAB TAHIR 11
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
}
default:{
System.out.println( "good bye");
break;
}
}
}
}
OUTPUT:
SHAHAB TAHIR 12
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
SHAHAB TAHIR 13
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
TASKNO#4: . Create a class of Automobile which contains specifications of a car, check whether the
car is in - ON/start state if not them asks the user if he/she want to start the car, If the car is already in
start state then first display the current status of the car which includes the horse power, color, made,
engine type etc. providing an option to the user if he/she wants to update any of the mentioned part from
the car, if user selects “YES” then it is updated according to the need of the user, else the program will be
ended.
SOLUTION:
MAIN CODE:
TALHA obj=new TALHA();
Scanner input=new Scanner(System.in);
obj.input();
obj.display();
boolean x=true;
while(x){
SHAHAB TAHIR 14
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
int choice;
if (car==true ) {
obj.input();
obj.display();
System.out.println("-------------------------------");
System.out.println("Do You Want To Update Your Car's Specifications? \n[1] Yes \n[2]
No ");
System.out.print("Enter Your Choice:");
choice1= input.nextInt();
if(choice1==1) {
System.out.println("What do you want to update\n 1.Name\n 2.Model\n 3.Mileage\n
4.Price");
int i;
i= input.nextInt();
obj.update(i);
obj.display();
System.out.println("----------------------------");
}
else if (choice1==2)
{
obj.display();
System.out.println("good bye");
}
SHAHAB TAHIR 15
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
else if (car==false)
{
System.out.println("WARNING!!!your car is in off state");
System.out.println("Do you want to start the car? \n[1] Yes \n[2] No ");
System.out.print("Enter Your Choice:");
choice= input.nextInt();
if(choice==1)
{
car=true;
obj.input();
obj.display();
System.out.println("Do You Want To Update Your Car's Specifications? \n[1] Yes
\n[2] No ");
System.out.print("Enter Your Choice:");
choice1= input.nextInt();
if(choice1==1) {
System.out.println("What do you want to update\n 1.Name\n 2.Model\n 3.Mileage\n
4.Price");
int i;
i= input.nextInt();
obj.update(i);
obj.display();
}
else if (choice1==2)
{
SHAHAB TAHIR 16
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
obj.display();
System.out.println("good bye");
}
}
}
System.out.println("Good Bye!!!");
CLASS CODE:
String Employeename;
String fathername;
int salary;
String designation;
}
public void display(){
System.out.println( "your Employee name is :"+Employeename);
SHAHAB TAHIR 17
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
}
public void update(int i){
switch(i){
case 1: {
System.out.println("Enter your Employee name :");
Employeename = input.next();
break;
}
case 2:
{
System.out.println( "Enter your father name :");
fathername=input.next();
break;}
case 3:{
System.out.println( "Enter your salary :");
salary=input.nextInt();
break;}
case 4:{
System.out.println( "Enter your designation :");
designation=input.next();
break;
}
default:{
System.out.println( "good bye");
SHAHAB TAHIR 18
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
break;
}
}
OUTPUT:
SHAHAB TAHIR 19
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
TASK NO#5: Implement a class Car, that has the following characteristics:
a) Brandname
b) PriceNew, which represents the price of the car when it was new
c) Color and
d) Odometer,which is milo meter shows number of milage travelled by car
The class should have:
A. A method getPriceAfterUse() which should return the price of the car after being used according to the
following formula: car price after being used=priceNew*(1-(odometer/600,00))
B. A method updateMilage (double travelled distance) that changes the current state of the car by
increasing its milage, and
C. A method outputDetails() that will output to the screen all the information of the car, i.e. brandname,
priceNew,price used,color and odometer.
SOLUTION:
MAIN CODE:
carsdata obj=new carsdata();
obj.getpriceafteruse();
obj.updatemilage();
obj.outputdetails();
System.out.println("GoodBye!!!");
CLASS CODE:
SHAHAB TAHIR 20
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
String brandname="Ferrari";
String color="Red";
double price=330000;
double odometer=100;
double used;
Scanner input=new Scanner(System.in);
public void getpriceafteruse()
{
used=price*(1-(odometer/60000));
System.out.println("Your Car's Price After Usage is:"+used);
}
public void updatemilage()
{
System.out.print("Enter Your Updated Mileage:");
odometer= input.nextDouble();
}
public void outputdetails()
{
System.out.println("-------CAR STATUS---------");
System.out.println("Your Car BrandName:"+brandname);
System.out.println("Your Car Color is:"+color);
System.out.println("Your Car Price is:"+price);
System.out.println("your Car Price after Usage is:"+used);
System.out.println("Your CAR odometer Status Is:"+odometer);
}
OUTPUT:
SHAHAB TAHIR 21
DATE :19-03-2021 Object Oriented Programming
[CLASSES AND OBJECTS ]
SHAHAB TAHIR 22