Programming Problem 1 For this problem, follow the instructions for Program #2 i
ID: 3768923 • Letter: P
Question
Programming Problem 1
For this problem, follow the instructions for Program #2 in Chapter 10 Exercises of your textbook. Keep the following points in mind:
Mick’s Wicks makes candles in various sizes. Create a class for the business named Candle that contains data fields for color, height, and price. Create get methods for all three fields. Create set methods for color and height, but not for price. Instead, when height is set, determine the price as $2 per inch. Create a child class named ScentedCandle that contains an additional data field named scent and methods to get and set it. In the child class, override the parent’s setHeight() method to set the price of a ScentedCandle object at $3 per inch. Write an application that instantiates an object of each type and displays the details. Save the files as Candle.java, ScentedCandle.java, and DemoCandles.java.
Programming Problem 2
For this problem, follow the instructions for Program #7 in Chapter 10 Exercises of your textbook. Keep the following points in mind:
Create a class named CarRental that contains fields that hold a renter’s name, zip code, size of the car rented, daily rental fee, length of rental in days, and total rental fee. The class contains a constructor that requires all the rental data except the daily rate and total fee, which are calculated, based on the size of the car: economy at $29.99 per day, midsize at $38.99 per day, or full size at $43.50 per day. The class also includes a display() method that displays all the rental data.
Create a subclass named LuxuryCarRental. This class sets the rental fee at $79.99 per day and prompts the user to respond to the option of including a chauffeur at $200 more per day. Override the parent class display() method to include chauffeur fee information. Write an application named UseCarRental that prompts the user for the data needed for a rental and creates an object of the correct type. Display the total rental fee.
Save the files as CarRental. java, LuxuryCarRental. java, and UseCarRental. java
Explanation / Answer
Problem 1
class Candle
{
String color;
double height;
double price;
void setColor(String x)
{
color=x;
}
void setHeight(double x)
{
height=x;
price= height*2;
}
String getColor()
{
return color;
}
double getHeight()
{
return height;
}
double price()
{
return price;
}
}
class ScentedCandle extends Candle
{
String scent;
void setScent(String x)
{
scent=x;
}
String getScent()
{
return scent;
}
void setHeight(double x)
{
height=x;
price= height*3;
}
}
class DemoCandles
{
public static void main(String[] args)
{
Candle c=new Candle();
c.setColor("red");
c.setHeight(20);
System.out.println("Price of "+c.getHeight()+"inch " +c.getColor()+" candle is "+c.price+"$");
ScentedCandle sc=new ScentedCandle();
sc.setScent("rose");
sc.setColor("red");
sc.setHeight(20);
System.out.println("Price of "+sc.getHeight()+"inch "+sc.getScent()+" "+sc.getColor()+" candle is "+sc.price+"$");
}
}
Problem 2:
import java.util.Scanner;
class CarRental
{
String name;
int code;
String size;
double rentalfee;
int days;
double totalrental;
CarRental(String x,int y,String z,int d)
{
name=x;
code=y;
size=z;
days=d;
if(size.equals("economy"))
{
rentalfee=29.99;
}
if(size.equals("midsize"))
{
rentalfee=38.99;
}
if(size.equals("full"))
{
rentalfee=43.50;
}
totalrental=rentalfee*days;
}
void display()
{
System.out.println("Total Rent of "+size+"Car on Renter's name"+name+" with zipcode"+code+" for "+days+" number of days is"+totalrental);
}
}
class LuxuryCarRental extends CarRental
{
double chauffeur ;
LuxuryCarRental(String x,int y,String z,int d,String r)
{
super(x,y,z,d);
rentalfee=79.99;
if(r.equals("yes"))
chauffeur=200;
else
chauffeur=0;
totalrental=rentalfee*days+chauffeur;
}
void Display()
{
System.out.println("Total Rent of Luxary Car on Renter's name"+name+" with zipcode"+code+" for "+days+" number of days is"+totalrental);
}
}
public class UseCarRental {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
System.out.println("Enter Renter's Name");
String name=s.next();
System.out.println("Enter zipcode");
int code=s.nextInt();
System.out.println("Enter Size of car(economy/midsize/full)");
String size=s.next();
System.out.println("Enter Rental days");
int ds=s.nextInt();
CarRental c=new CarRental(name,code,size,ds);
c.display();
System.out.println("would you like to include chauffeur fee information(yes/no)");
String response=s.next();
LuxuryCarRental lc=new LuxuryCarRental(name,code,size,ds,response);
lc.Display();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.