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

Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has...
Java assignment, abstract class, inheritance, and polymorphism. these are the following following classes: Animal (abstract) Has a name property (string) Has a speech property (string) Has a constructor that takes two arguments, the name and the speech It has getSpeech() and setSpeech(speech) It has getName and setName(name) It has a method speak() that prints the name of the animal and the speech. o Example output: Tom says meow. Mouse Inherits the Animal class Has a constructor takes a name and...
I need to see a working example using inheritance and the classes (below) The person class...
I need to see a working example using inheritance and the classes (below) The person class has first name last name age hometown a method called getInfo() that returns all attributes from person as a String The student class is a person with an id and a major and a GPA student has to call super passing the parameters for the super class constructor a method called getInfo() that returns all attributes from student as a String this methods has...
In this project you will implement the DataFrame class along with the all the methods that...
In this project you will implement the DataFrame class along with the all the methods that are represented in the class definition. DataFrame is a table with rows and columns – columns and rows have names associated with them also. For this project we will assume that all the values that are stored in the columns and rows are integers. After you have tested your class, you have to execute the main program that is also given below. DataFrame Class...
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...
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...
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...
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:...
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?...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT