Question

I want to write a superclass with one private attribute and one method in JAVA The super class wi...

I want to write a superclass with one private attribute and one method in JAVA

The super class will later derive into 2 subclasses each with its own private attributes and each with a method that OVERRIDES the superclass method with each subclasses with one method ( that will do something) unique to that subclass. ALL classes must have constructors initializing its attributes and getter/setter methods. Then, in the main method,  keep initializing objects of type superclasses based on user's choice and add them to the ArrayList.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Program

import java.io.IOException;
import java.util.ArrayList;

class Car{//super class
   private int price;
  
   public Car(int price) {
       this.price = price;
   }
   public int getPrice() {
       return price;
   }
   public void setPrice(int price) {
       this.price = price;
   }
   public double SalesPrice() {
       double totalPrice = price * 0.1;// 10% discount
       return price-totalPrice;
   }
}

class MVP extends Car{
   private int length;
  
   public MVP(int price, int length) {
       super(price);
       this.length = length;
   }

   @Override
   public double SalesPrice() {//calculating the sales price
       double discountPrice;
       if(length>15) {
           discountPrice = getPrice() * 0.025;//2.5% discount
       }
       else {
           discountPrice = getPrice() * 0.05;//5% discount
       }
       return getPrice() - discountPrice;
   }
}

class Sedan extends Car{
   private int manufacturerDiscount;
  
   public Sedan(int price, int manufacturerDiscount) {
       super(price);
       this.manufacturerDiscount = manufacturerDiscount;
   }

   @Override
   public double SalesPrice() {//deducting manufacturer discount
       double totalPrice = super.SalesPrice() - manufacturerDiscount;
       return totalPrice;
   }
}

public class KuchBhi {// driver class
   public static void main(String[] args)throws IOException {// driver method
       ArrayList<Car> ar = new ArrayList<>();
       //adding objects of subclasses
       ar.add(new MVP(1500, 20));
       ar.add(new Sedan(3000, 750));
       //showing result
       System.out.println("MVP Sales Price: "+ar.get(0).SalesPrice());
       System.out.println("Sedan Sales Price: "+ar.get(1).SalesPrice());
   }
}

Program Screenshot

KuchBhi.java 1 import java.io.IOException; 2 import java.uti1.ArrayList; 4 class Car//super class private int price; 7 publicKuchBhi.java 63 64 65 ar.add(new Sedan (3000, 750)); //showing result System.out.println(MVP Sales Price: +ar.get (ø).Sales

Output

<terminated> KuchBhi (1) Java Application] C:Program FilesJava jre1.8.0_181 bin javaw.exe (08-Apr-2019, 3:01:21 PM) MVP Sales

Add a comment
Know the answer?
Add Answer to:
I want to write a superclass with one private attribute and one method in JAVA The super class wi...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java Which of the following statements declares Salaried as a subclass of payType? Public class...

    In Java Which of the following statements declares Salaried as a subclass of payType? Public class Salaried implements PayType Public class Salaried derivedFrom(payType) Public class PayType derives Salaried Public class Salaried extends PayType If a method in a subclass has the same signature as a method in the superclass, the subclass method overrides the superclass method. False True When a subclass overloads a superclass method………. Only the subclass method may be called with a subclass object Only the superclass method...

  • (JAVA) Write an abstract superclass encapsulating a college    applicant: A college applicant has two attributes:...

    (JAVA) Write an abstract superclass encapsulating a college    applicant: A college applicant has two attributes:    the applicant’s name and the college the applicant    is applying to. This class has two non-abstract    subclasses: one encapsulating an applicant for    undergraduate school, and the other encapsulating    an applicant for graduate school. An applicant for    undergraduate school has two attributes: a SAT score    and a GPA. An applicant for graduate school has one    additional attribute:...

  • Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string...

    Create the Employee class: The Employee class has three attributes: private: string firstName; string lastName; string SSN; The Employee class has ▪ a no-arg constructor (a no-arg contructor is a contructor with an empty parameter list): In this no-arg constructor, use “unknown” to initialize all private attributes. ▪ a constructor with parameters for each of the attributes ▪ getter and setter methods for each of the private attributes ▪ a method getFullName() that returns the last name, followed by a...

  • What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that...

    What is the code for this in Java? Assignment Inheritance Learning Objectives Declare a subclass that derives from a superclas:s ■ Demon "Declare a variable of the superclass type and assign it an instance of the subclass type strate polymorphic behavior Access the public members of the superclass type Notice how the overridden versions of the subclass type are called Notice how the subclass specific members are inaccessible "Create an array of superclass type and use a foreach loop to...

  • please this is Java and i need help Question 5 public class Food { public void...

    please this is Java and i need help Question 5 public class Food { public void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { } public static void Foodmethod_3(int i) { public class Bankudade extends Food { public static void Foodmethod_1 (int i) { public void Foodmethod_2 (int i) { public void Foodmethod_3 (int i) { > 7 a) Determine which method in the subclass overrides a method in the super class? (6 marks) EV b) Determine which...

  • Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number...

    Write a Java class that implements the concept of Coins, assuming the following attributes (variables): number of quarters, number of dimes, number of nickels, and number of pennies. Include two constructors (non-argument constructor that assigns 1 to each coin, and another constructor that takes necessary arguments to create an object), needed getter and setter methods, method toString (to print coins objects in a meaningful way), and method equals (to compare two coins objects based on number of coins). Also include...

  • Here is a sample run of the tester program: Make sure your program follows the Java...

    Here is a sample run of the tester program: Make sure your program follows the Java Coding Guidelines. Consider the following class: /** * A store superclass with a Name and Sales Tax Rate */ public class Store {      public final double SALES_TAX_RATE = 0.06;      private String name;           /**      * Constructor to create a new store      * @param newName      */      public Store(String newName)      {           name = newName;      }     ...

  • Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman...

    Implement an abstract class named Person and two subclasses named Student and Staff in Java. A person has a name, address, phone number and e-mail address. A student has a credit hour status (freshman, sophomore, junior, or senior). Define the possible status values using an enum. Staff has an office, salaray, and date-hired. Implement the above classes in Java. Provide Constructors for classes to initialize private variables. Getters and setters should only be provided if needed. Override the toString() method...

  • Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and...

    Java Create four classes: 1.      An Animal class that acts as a superclass for Dog and Bird 2.      A Bird class that is a descendant of Animal 3.      A Dog class that is a descendant of Animal 4.      A “driver” class named driver that instantiates an Animal, a Dog, and a Bird object. The Animal class has: ·        one instance variable, a private String variable named   name ·        a single constructor that takes one argument, a String, used to set...

  • Write an abstract superclass encapsulating a vehicle: A vehicle has two attributes: its owner's name and its number of wheels. This class has two non-abstract subclasses: one encapsulating a bicyc...

    Write an abstract superclass encapsulating a vehicle: A vehicle has two attributes: its owner's name and its number of wheels. This class has two non-abstract subclasses: one encapsulating a bicycle, and the other encapsulating a motorized vehicle. A motorized vehicle has the following additional attributes: its engine volume displacement, in liters; and a method computing and returning a measure of horsepower which is the number of liters times the number of wheels. Write an application class utilizing these two classes.

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT