Oop Lab Assignment 5

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

CSL-210: Object-Oriented Programming Lab

BS(CS)- Semester 02
(Fall 2022)

Lab05: Classes and Objects

Designing and implementing Java programs that deal with:


1. A class Definition
2. Constroctor
3. Class members
4. Class methods
5. The new Operator to create a new object
6. The this Operator

Exercises

Exercise 1 (Book.java)

Write a Java class Book with following features:

 Private Instance variables :


o title for the title of book of type String.
o author for the author’s name of type String.
o price for the book price of type double.
 Constructor:
o public Book (String title, Author name, double price): A constructor with
parameters, it creates the Author object by setting the the fields to the passed
values. Note that values should be assigned via the setter methods created for
each varible
 Instance methods:
o public void setTitle(String title): Used to set the title of book.
o public void setAuthor(String author): Used to set the name of author of book.
o public void setPrice(double price): Used to set the price of book.
o public double getTitle(): This method returns the title of book.
o public double getAuthor(): This method returns the author’s name of book.
o public String toString(): This method printed out book’s details to the screen
Write a separate class BookDemo with a main() method creates a Book titled “Developing Java
Software” with authors Russel Winderand price 79.75. Prints the Book’s string representation to
standard output (using System.out.println).
CS Department, BUKC 2/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

CODE:
package book;

import java.util.Scanner;

public class Book {


private String title;
private String author;
private double price;

public Book(String title, String author, double price) {


this.setAuthor(author);
this.setPrice(price);
this.setTitle(title);
}

public String getTitle() {


return title;
}

public String getAuthor() {


return author;
}

public double getPrice() {


return price;
}

public void setTitle(String title) {


this.title = title;
}

public void setAuthor(String author) {


this.author = author;
}

public void setPrice(double price2) {


this.price = price2;
}

public String toString() {


System.out.println("\nBOOK DETAILS");
System.out.println("Book Name : " + this.getTitle());
CS Department, BUKC 3/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

System.out.println("Book Author : " + this.getAuthor());


System.out.println("Book Price : " + this.getPrice());
return null;
}

public static void main(String[] args) {


Scanner scan = new Scanner(System.in);
System.out.print("Enter book title : ");
String title=scan.nextLine();
System.out.print("Enter author name : ");
String author=scan.nextLine();
System.out.print("Enter book price : ");
double price = scan.nextDouble();

Book b1=new Book(title, author, price);


b1.toString();

}
}
OUTPUT:
CS Department, BUKC 4/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

Exercise 2(a) (Car.java)

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 ( )

B. A method updateMilage(double traveledDistance) 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.,
brand name, price new, price used, color, and odometer.

CODE:
package car;

import java.util.Scanner;

public class CAR {

String brand_name;
String Color;
long new_price;
double odometer;

public double getPriceAfterUse() {


return new_price * (1 - (odometer / 600000));
CS Department, BUKC 5/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

public void updatemileage(double traveledDistance) {


odometer = odometer + traveledDistance;
}

public void OutputDetails() {


System.out.println("\nYour Car Information:");
System.out.println("Brand: " + brand_name);
System.out.println("Color: " + Color);
System.out.println("New Price: " + new_price);
System.out.println("Odometer: " + odometer);
System.out.println("Price After use: " + getPriceAfterUse() + "\n");
}

public static void main(String[] args) {


CAR details = new CAR();
Scanner input = new Scanner(System.in);
System.out.print("Enter Brand: ");
details.brand_name = input.nextLine();
System.out.print("Enter Color: ");
details.Color = input.nextLine();
System.out.print("Enter New Price: ");
details.new_price = input.nextInt();
System.out.print("Enter Odometer: ");
details.odometer = input.nextInt();
details.updatemileage(20000);
details.OutputDetails();
}

}
CS Department, BUKC 6/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

OUTPUT:
CS Department, BUKC 7/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

Exercise 2(b) (TestCar.java)

Write a test class for the Car class above. You are required to do the followings:
a. Create an object of type Car.
b. Assign any valid values to the instance variables of the object created in ‘A’.
c. Use the method getPriceAfterUse on the object created in ‘A’ then output the result to the
screen.
d. Use the method updateMilage on the object created in ‘A’ by passing a valid value.
e. Do part ‘C’ again.
f. Use the method outputDetails on the object created in ‘A’.

CODE:
package testcar;

import java.util.Scanner;

public class TESTCAR {

String brand_name = "TOYOTA";


String Color = "WHITE";
double new_price = 2000000;
double odometer = 150000;

public double getPriceAfterUse() {


return new_price * (1 - (odometer / 600000));
}

public void updatemilage(double traveledDistance) {


odometer = odometer + traveledDistance;
}

public void OutputDetails() {


System.out.println("Brand: " + brand_name);
System.out.println("Color: " + Color);
System.out.println("New Price: " + new_price);
System.out.println("Odometer: " + odometer);
CS Department, BUKC 8/8 Fall 2022
CSL-210: Object-Oriented Programming Lab Lab05: Classes and Objects

System.out.println("Price After use: " + getPriceAfterUse() + "\n");


}

public static void main(String[] args) {


TESTCAR car = new TESTCAR();
car.OutputDetails();
car.updatemilage(10000);
car.OutputDetails();
car.updatemilage(20000);
car.OutputDetails();
}

}
OUTPUT:

You might also like