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

This assignment tests your understanding of abstract classes, inheritance, and requirements modeling using class-based methods. The...
This assignment tests your understanding of abstract classes, inheritance, and requirements modeling using class-based methods. The assignment scenario involves an abstract vehicle class and concrete subclasses Jeep and Ford. The vehicle class has the following variables (manufacturer, language), and methods (getManufacturer( ), and getLanguage( )). The manufacturer builds a specific vehicle (a Jeep and a Ford). The manufacturer will notify the consumer class that a Jeep and a Ford are available for purchase. The consumer class purchases the Jeep. Your...
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...
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...
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...
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT