Question

In: Computer Science

Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods) -For all classes, you need to provide...

Chapter 9 Programming Project (Inheritance, Abstract Class/Methods, Overriding Methods)

-For all classes, you need to provide the accessor and mutator methods for all instance variables, and provide/override the toString methods.

-Create a Customer class that has the attributes of name and age. Provide a method named importanceLevel. Based on the requirements below, I would make this method abstract.

-Extend Customer to two subclasses: FlightCustomer, and RetailCustomer

-FlightCustomer attributes: ticketPrice, seatNumber (The seat number should be a randomly generated number between 1 to 200)

-RetailCustomer attributes: itemsPurchased, totalSpent.

-For both FlighCustomer and RetailCustomer, you need to provide the implementation for the importanceLevel method. There are four levels: gold, silver, bronze, regular. For FlighCustomer, the level is based on the ticketPrice; for RetailCustomer, the level is based on the average price of each item.

-Instantiate three Customers for each class (six allotter) and tests ALL methods in a meaningful/informative way.

-Programming Language is Java

Solutions

Expert Solution

Java Code:

import java.util.Random;
abstract class Customer{
String name;
int age;
Customer(String name,int age){
this.name=name;
this.age=age;
}
//getter method for name
public String getName() {
return name;
}
//getter method for age
public int getAge() {
return age;
}

// Setter method for name
public void setName(String Name) {
this.name = Name;
}
//setter method for age
public void setAge(int Age) {
this.age = Age;
}
// abstract method
abstract void importanceLevel();
//toString method
public String toString()
{
return ("The Persons's name is : "+name + " & age is : " + age );
}
}
class FlightCustomer extends Customer{
int ticketPrice, seatNumber;
Random rand = new Random();
  
// Generate random integers in range 0 to 200
int rand1 = rand.nextInt(200);
int rand2 = rand.nextInt(200);
FlightCustomer(String name,int age,int ticketPrice){
super(name,age);
this.ticketPrice= ticketPrice;
this.seatNumber=rand1;
  
}
//getter method
public int getTicketPrice() {
return ticketPrice;
}
//getter method
public int getSeatNumber() {
return seatNumber;
}

// Setter method
public void setTicketPrice(int ticketPrice) {
this.ticketPrice=ticketPrice;
}
//setter method
public void setSeatNumber() {
this.seatNumber = rand2;
}
//toString method
public String toString()
{
return ("The Persons's name is : "+name + " & age is : " + age + " & TicketPrice is " +ticketPrice +" & seatnumber is : " +seatNumber );
}
  
//here we implementing abstract method of customer class
public void importanceLevel(){
if(ticketPrice>500){
System.out.println("Level is gold");
}
if(ticketPrice>=400 && ticketPrice<500){
System.out.println("Level is silver");
}
if(ticketPrice>=300 && ticketPrice<400){
System.out.println("Level is bronze");
}
if(ticketPrice<300){
System.out.println("Level is regular");
}

}
}
class RetailCustomer extends Customer{
int itemsPurchased, totalSpent;
RetailCustomer(String name,int age,int itemsPurchased,int totalSpent){
super(name,age);
this.itemsPurchased=itemsPurchased;
this.totalSpent=totalSpent;
  
}
//getter method
public int getItemsPurchased() {
return itemsPurchased;
}
//getter method
public int getTotalSpent() {
return totalSpent;
}

// Setter method
public void setItemsPurchased(int itemsPurchased) {
this.itemsPurchased=itemsPurchased;
}
//setter method
public void setTotalSpent(int totalSpent) {
this.totalSpent=totalSpent;
}
//toString method
public String toString()
{
return ("The Persons's name is : "+name + " & age is : " + age +" & itemPurchased is : "+ itemsPurchased+" & totalspent is : " +totalSpent);
}
//here we implementing abstract method of customer class
public void importanceLevel(){
int avgPrice;
//if itemPurchased is zero.then avgPrice will be undefined.so we handel that case using the following try catch block
try {
avgPrice = totalSpent / itemsPurchased;

}
catch(ArithmeticException e){
System.out.println("Item purchased can not be zero");
return;
}
  
if(avgPrice>500){
System.out.println("Level is gold");
}
if(avgPrice>=400 && avgPrice<500){
System.out.println("Level is silver");
}
if(avgPrice>=300 && avgPrice<400){
System.out.println("Level is bronze");
}
if(avgPrice<300){
System.out.println("Level is regular");
}

}
}

public class HelloWorld{

public static void main(String []args){
  
// here we create 3 FlightCustomer objects
FlightCustomer f1=new FlightCustomer("Ram",80,9000);
FlightCustomer f2=new FlightCustomer("sam",29,199);
FlightCustomer f3=new FlightCustomer("jodu",26,400);
System.out.println(f1.toString());
f1.importanceLevel();
f1.setName("Ravan");
f1.setSeatNumber();
f2.setAge(36);
System.out.println(f1.toString());
System.out.println(f2.toString());
f2.importanceLevel();
System.out.println(f3.toString());
f3.importanceLevel();
  
  
// here we create 3 RetailedCustomer objects
RetailCustomer r1=new RetailCustomer("sanu",20,50,600);
RetailCustomer r2=new RetailCustomer("hanu",30,10,7000);
RetailCustomer r3=new RetailCustomer("bonu",40,15,300);
System.out.println(r1.toString());
r1.importanceLevel();
r1.setName("shreya");
r1.setAge(45);
r1.setItemsPurchased(5);
r1.setTotalSpent(700);

System.out.println(r1.toString());
System.out.println(r2.toString());
r2.importanceLevel();
System.out.println("honu's age is : "+r2.getAge());
System.out.println(r3.toString());
System.out.println("bonu's totalspent is : "+r3.getTotalSpent());
r3.importanceLevel();
}
}

OUTPUT:

Code in my Complier:


Related Solutions

Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all...
Shapes2D Write the following four classes to practice using an abstract class and polymorphism. Submit all four classes. Shape2D class For this class, include just an abstract method name get2DArea() that returns a double. Rectangle2D class Make this class inherit from the Shape2D class. Have it store a length and a width as fields. Provide a constructor that takes two double arguments and uses them to set the fields. Note, the area of a rectangle is the length times the...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and...
java programming You will be given two interfaces and two abstract classes, FileTextReader, FileTextWriter, AbstractFileMonitor, and AbstractDictionary. Your job is to create two classes the first class should be named FileManager, the second class should be named Dictionary. The FileManager will implement the interfaces FileTextReader and FileTextWriter and extend the clas AbstractFileMonitor. Your class signature would look something like the following: public class FileManager extends AbstractFileMonitor implements FileTextReader, FileTextWriter{... The constructor signature of the FileManager should look like the following:...
Chapter 9 project 1 & 2(these are combined into one project) : Add methods to the...
Chapter 9 project 1 & 2(these are combined into one project) : Add methods to the Student class that compare two Student objects. One method should test for equality. The other methods should support the other possible comparisons. In each case, the method returns the result of the comparison of the two students' names. Place several Student objects into a list and shuffle it. Then run the sort method with this list and display all of the students' information. I...
A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.
1. Java Abstract Class (2 pts) () • A Java abstract class is a class that can't be instantiated. That means you cannot create new instances of an abstract class. It works as a base for subclasses. You should learn about Java Inheritance before attempting this challenge.• Following is an example of abstract class:abstract class Book{String title;abstract void setTitle(String s);String getTitle(){return title;}}If you try to create an instance of this class like the following line you will get an error:Book...
Design You will need to have at least four classes: a parent class, a child class,...
Design You will need to have at least four classes: a parent class, a child class, a component class, and an unrelated class. The component object can be included as a field in any of the other three classes. Think about what each of the classes will represent. What added or modified methods will the child class have? What added fields will the child class have? Where does the component belong? How will the unrelated class interact with the others?...
Chapter 9 In Class Exercises: Depreciation Methods Scenario: Cost = Useful Life (Years) = Useful Life...
Chapter 9 In Class Exercises: Depreciation Methods Scenario: Cost = Useful Life (Years) = Useful Life (Hours) = Salvage Value = a) Calculate the SL annual rate: 30,000 5 140,000 2,000 1) Depreciable Cost = Cost - Salvage ValueDepreciable Cost = - 2) Calculate the Double DB rate: SL Rate x2 - = Double DB rate b) Calculate depreciation expense, accumulated depreciation and book value for the life of the asset. Depreciable Depreciation Annual Depr Accum. Year Cost Rate Exp...
Chapter 9 thoroughly discusses the circumstances under which and the methods for reducing project duration. Section...
Chapter 9 thoroughly discusses the circumstances under which and the methods for reducing project duration. Section 9.2 explains 10 options for accelerating project completion: Adding resources Outsourcing project work Scheduling overtime Establish a core project team Do it twice - fast and correctly Improve the efficiency of the project team Fast-tracking Critical-chain Reducing project scope Compromising quality I want you to think about a time where you have executed one of these methods at work when a project was taking...
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker...
This assignment assumes you have completed Programming Challenge 1 of Chapter 9 ( Employee and ProductionWorker Classes). Modify the Employee and ProductionWorker classes so they throw exceptions when the following errors occur: ● The Employee class should throw an exception named InvalidEmployeeNumber when it receives an employee number that is less than 0 or greater than 9999. ● The ProductionWorker class should throw an exception named InvalidShift when it receives an invalid shift. ● The ProductionWorker class should throw an...
You are given the following class definition (assume all methods are correctly implemented): public class Song...
You are given the following class definition (assume all methods are correctly implemented): public class Song { ... public Song(String name, String artist) { ... } public String getName() { ... } public String getArtist() { ... } public String getGenre() { ... } public int copiesSold() { ... } } Write NAMED and TYPED lambda expressions for each of the following, using appropriate functional interfaces from the java.util.function and java.util packages (Only the lambda expression with no type+name will...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT