Question

Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also make a constructor to initializ
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here I am providing the answers for above questions:

Question1:

public class Car
{
   String model,color;
   double price;
   public Car(String model,String color,double price)
   {
       this.model=model;
       this.color=color;
       this.price=price;
   }
   public double getPrice()
{
return price;
}
}
class Truck extends Car
{
int weight;
public Truck(String model,String color,double price,int weight)
{
super(model,color,price); //calling the parent constructro for initializing the values
this.weight=weight;
}
public double getPrice()
{
if(super.price>20000)   
{
double discount=super.price*0.2;
return super.price-discount;
  
}
else
return super.price;
}
public static void main(String[] args)
{
Truck t=new Truck("abc","blue",30000,200); //creating the Object of Truct for checking
System.out.println("Price:"+t.getPrice());
}
}

Question2:

public abstract class Animal
{
String name;
int age,sleepHours;
public abstract void makeNoise();
public abstract void sleep(int hours);
public Animal(String name,int age,int sleepHours)
{
this.name=name;
this.age=age;
this.sleepHours=sleepHours;
}
}
class Elephant extends Animal
{
public Elephant(String name,int age,int sleepHours)
{
super(name,age,sleepHours);
}
public void makeNoise() //implementing the function of parent class
{
System.out.println("make noise function");
}
public void sleep(int hours) //implementing the function of parent class
{
System.out.println("Sleep Hours:"+hours);
}
public static void main(String[] args)
{
Elephant e=new Elephant("abc",20,8); //creating the object of Elephatn class for checking.
e.sleep(e.sleepHours);
}
}

Question3:

public interface Employee //Employee Interface
{
int retirementAge=60;
public double generateSalary();
public double getBonus(double salary);

}
class Manager implements Employee
{
public double getBonus(double salary)
{
return salary+(salary*0.2); //with 20% bonus
}
public double generateSalary()
{
int salary=33000;
return salary;
}
public static void main(String[] args)
{
Manager m=new Manager();
System.out.println("Salary generated for manager:"+m.generateSalary());
}
}
class Staff implements Employee
{
public double getBonus(double salary)
{
return (salary*0.1); //with 10% bonus
}
public double generateSalary()
{
int salary=20000;
return salary;
}
public static void main(String[] args)
{
Staff s=new Staff();
System.out.println("bonus for staff:"+s.getBonus(20000));
}
}

Hoping that the above code will help you...Thank you...

Add a comment
Know the answer?
Add Answer to:
Question #1 (CLO: 1,2.3) Make a class called Car. Declate its data, model, colour, price Also...
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 Create an interface called Amount that includes a method called setPrice (). Create an a...

    In Java Create an interface called Amount that includes a method called setPrice (). Create an abstract class named Book that inherits from the interface Amount. Include a String field for the book’s title and a double field for the book’s Price . Within the class, include a constructor that requires the book title and add two getter methods — one that returns the title and one that returns the price. Include an abstract method named setPrice (). Create a...

  • Need help to create general class Classes Data Element - Ticket Create an abstract class called...

    Need help to create general class Classes Data Element - Ticket Create an abstract class called Ticket with: two abstract methods called calculateTicketPrice which returns a double and getld0 which returns an int instance variables (one of them is an object of the enumerated type Format) which are common to all the subclasses of Ticket toString method, getters and setters and at least 2 constructors, one of the constructors must be the default (no-arg) constructor. . Data Element - subclasses...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • Create an Item class, which is the abstract super class of all Items.           Item class...

    Create an Item class, which is the abstract super class of all Items.           Item class includes an attribute, description of type String for every item. [for eg. Book]                                                             A constructor to initialize its data member of Item class.               Override toString() method to return details about the Item class. Create the ExtraCharge interface with the following details.                       Declare a constant RATE with value 0.25   Declare a method called calculateExtraCharge(), which returns a double value. Create the...

  • In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a...

    In this lab you will work with abstract classes/interfaces. (Java Program) You will be implementing a basic employee schema within a company. The Employee class will be an abstract class that will contain methods applicable to all employees. You will then create 2 classes called SoftwareEngineer and ProductManager. Both are different employee types based on occupation. You will create an interface called Developer which will consist of specific methods that apply to only Developers (e.g. SoftwareEngineer class will implement this,...

  • Ticket Hierarchy Ticket Abstract Class Create a static variable called nextTicketId. This is an integer representing...

    Ticket Hierarchy Ticket Abstract Class Create a static variable called nextTicketId. This is an integer representing the next available integer for the ticketId                                 private static int nextTicketId = 1000; getPrice method: Abstract method that returns a double. NOTE: Do not make price an instance variable in Ticket. Noargument constructor: Sets event name to “none”, event location to “none”, and ticket id to 0. Create a constructor that accepts the event name and event location as parameters. Set the ticket Id...

  • cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher,...

    cs55(java) please. Write a class called Book that contains instance data for the title, author, publisher, price, and copyright date. Define the Book constructor to accept and initialize this data. Include setter and getter methods for all instance data. Include a toString method that returns a nicely formatted, multi-line description of the book. Write another class called Bookshelf, which has name and array of Book objects. Bookself capacity is maximum of five books. Includes method for Bookself that adds, removes,...

  • Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables,...

    Question 4: CLO5 Write a class called Rectangle that has length and width as instance variables, a constructor with two parameter to initialize length and width, set and get methods for each variables and the following methods: a. The first method calculates and returns the value of the ratio of the length to the width of the rectangle. b. The second method determines whether a rectangle is a Square depending on the value of the ratio, which should be calculated...

  • This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets...

    This code is in java. Create a Java class that has the private double data of length, width, and price. Create the gets and sets methods for the variables. Create a No-Arg constructor and a constructor that accepts all three values. At the end of the class add a main method that accepts variables from the user as input to represent the length and width of a room in feet and the price of carpeting per square foot in dollars...

  • 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of...

    in java ..write all complete program from a- e 1. Given the two binary trees below: 14 16 Write a method called swapSubtrees, which swaps all of the left and right subtrees in the above binary trees. Add this method to the class BinaryTree and create a program to test this method for these 2 trees. Show the original trees and the resulting trees. Note: To test your algorithm, first create a binary search tree. Write a method called singleParent,...

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