By GokiSoft.com| 09:58 19/07/2021|
Java Basic

Java basic- Overview - Viết chương trình quản lý xe car bằng java BT1000

1. Create an interface Icar that has the following methods:                                        

      Method “calculateTax()”, returns the type float: calculate the tax of the car.

      Method “calculatePrice()”, returns the type float: calculate the total cost of the car.                                                                      

      Method “getInfor()”, returns the type void: display the information of the car.

 

2. Create a class Car inherit from interface Icar.                                                                                         [6]

Declare fields has access modifier as private use for properties: name, producer, year, seat and rootprice.                                                                          

 

 

        Create two constructors and all method get/set for its attributes

        Implement method calculateTax() to calculate the Tax as follows:            

    If the car has under 7 seats then tax = RootPrice * 60%.

    Else tax = RootPrice * 70%

 

        Implement method calculatePrice() to calculate TotalPrice as follows:      

    TotalPrice=RootPrice  + Tax

 

        Implement method getInfor() to show the information as follows: .... car produced by ... in ... has ... seats with the total price is ....$. Variable includes :name of the car, the name of the manufacturer, year of production, seats and TotalPrice (For example: Ford car produced by Ford in 1997 has 4 seats with the total price is 20000 $).                                                                               

 
  1. Declaring class LuxuryCar inherits from class Car and adding follow attrubtes :                           

private float specialRate;

 

        Create two constructors and all method get/set for its attributes

        Override method calculatePrice to calculate TotalPrice as follows :          

TotalPrice = RootPrice + Tax + RootPrice * specialRate

   

        Overload method calculatePrice with 1 parameter named “transportCost” with type float. It calculates TotalCost as follows:                                                   TotalPrice = RootPrice + Tax + RootPrice * specialRate + transportCost.

   
  1. Create a class named Test.                                                                           

        Declare and initialize 1 instance named “myLuxuryCar” of class LuxuryCar.

    Input Name, Producer, Year, Seat, rootPrice from the keyboard (Catch exception when input value). Display the contents of this LuxuryCar.

    Calculate TotalPrice in case transportCost is $ 20,000, and display it.

Liên kết rút gọn:

https://gokisoft.com/1000

Bình luận

avatar
GokiSoft.com [Teacher]
2021-07-23 02:00:45


#Main.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

/**
 *
 * @author Diep.Tran
 */
public class Main {
    public static void main(String[] args) {
        LuxuryCar myLuxuryCar = new LuxuryCar();
        myLuxuryCar.input();
        
        myLuxuryCar.getInfor();
        
        float total = myLuxuryCar.calculatePrice(20000);
        System.out.println("\nTotal Price: " + total);
    }
}


#LuxuryCar.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class LuxuryCar extends Car{
    float specialRate;

    public LuxuryCar() {
    }

    public LuxuryCar(float specialRate, String name, String producer, String year, int seat, float rootPrice) {
        super(name, producer, year, seat, rootPrice);
        this.specialRate = specialRate;
    }

    @Override
    public float calculatePrice() {
        return getRootPrice() + calculateTax() + getRootPrice() * specialRate;
    }
    
    public float calculatePrice(float transportCost) {
        return calculatePrice() + transportCost;
    }

    @Override
    public void input() {
        super.input(); //To change body of generated methods, choose Tools | Templates.
        Scanner scan = new Scanner(System.in);
        
        try {
            System.out.println("Special Rate: ");
            specialRate = Float.parseFloat(scan.nextLine());
        } catch(NumberFormatException e) {
        }
    }
}


#ICar.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

/**
 *
 * @author Diep.Tran
 */
public interface ICar {
    float calculateTax();
    float calculatePrice();
    void getInfor();
}


#Car.java


/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package lesson09;

import java.util.Scanner;

/**
 *
 * @author Diep.Tran
 */
public class Car implements ICar{
    String name, producer, year;
    float rootPrice;
    int seat;

    public Car() {
    }

    public Car(String name, String producer, String year, int seat, float rootPrice) {
        this.name = name;
        this.producer = producer;
        this.year = year;
        this.seat = seat;
        this.rootPrice = rootPrice;
    }
    
    @Override
    public float calculateTax() {
        if(seat <= 7) {
            return rootPrice * 0.6f;
        }
        return rootPrice * 0.7f;
    }

    @Override
    public float calculatePrice() {
        return rootPrice + calculateTax();
    }

    @Override
    public void getInfor() {
        System.out.printf("\n%s car produced by %s in %s "
                + "has %d seats with the total price is %f$", 
                name, producer, year, seat, calculatePrice());
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public int getSeat() {
        return seat;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public float getRootPrice() {
        return rootPrice;
    }

    public void setRootPrice(float rootPrice) {
        this.rootPrice = rootPrice;
    }
    
    public void input() {
        Scanner scan = new Scanner(System.in);
        
        System.out.println("Name: ");
        name = scan.nextLine();
        
        System.out.println("Producer: ");
        producer = scan.nextLine();
        
        System.out.println("Year: ");
        year = scan.nextLine();
        
        try {
            System.out.println("Root Price: ");
            rootPrice = Float.parseFloat(scan.nextLine());
        } catch(NumberFormatException e) {
        }
        
        try {
            System.out.println("Seat: ");
            seat = Integer.parseInt(scan.nextLine());
        } catch(NumberFormatException e) {
        }
    }
}


avatar
Hoàng Quang Huy [C1907L]
2020-03-31 14:04:06





package Lesson_30_3_2020;

public interface ICar {
	public float calculateTax();
    public float calculatePrice();
    public void getInfor();
}
-------------------------------------------------------------------------------------
package Lesson_30_3_2020;

import java.util.Scanner;

public class Car implements ICar {
	private String name;
	private String producer;
	private int year;
	private int seat;
	private float rootprice;

	public Car() {
		super();
	}

	public Car(String name, String producer, int year, int seat, float rootprice) {
		super();
		this.name = name;
		this.producer = producer;
		this.year = year;
		this.seat = seat;
		this.rootprice = rootprice;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getProducer() {
		return producer;
	}

	public void setProducer(String producer) {
		this.producer = producer;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public int getSeat() {
		return seat;
	}

	public void setSeat(int seat) {
		this.seat = seat;
	}

	public float getRootprice() {
		return rootprice;
	}

	public void setRootprice(float rootprice) {
		this.rootprice = rootprice;
	}

	@Override
	public float calculateTax() {
		if (this.seat < 7 && this.seat > 0) {
			return (float) (this.rootprice * 0.6);
		} else {
			return (float) (this.rootprice * 0.7);
		}
	}

	@Override
	public float calculatePrice() {
		float totalprice = this.rootprice + calculateTax();
		return totalprice;
	}

	@Override
	public void getInfor() {
		System.out.println(this.name + " car produced by " + this.producer + " in " + this.year + " has " + this.seat
				+ " seats with the total price is " + this.calculatePrice() + "$");

	}

	public void setInfor() {
		Scanner input = new Scanner(System.in);
		System.out.println("Car Name: ");
		this.name = input.nextLine();
		System.out.println("Producer Name: ");
		this.producer = input.nextLine();
		while(true) {
			try {
				System.out.println("Year produced: ");
				this.year = Integer.parseInt(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid Year");
			}
		}
		while(true) {
			try {
				System.out.println("Number of seats: ");
				this.seat = Integer.parseInt(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid number of seats");
			}
		}
		while(true) {
			try {
				System.out.println("Root Price: ");
				this.rootprice = Float.parseFloat(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid Root Price");
			}
		}
	}
}
------------------------------------------------------------------------------------
package Lesson_30_3_2020;

import java.util.Scanner;

public class LuxuryCar extends Car{
	private float specialRate;

	public LuxuryCar(float specialRate) {
		super();
		this.specialRate = specialRate;
	}

	public LuxuryCar() {
		super();
	}
	
	public float getSpecialRate() {
		return specialRate;
	}

	public void setSpecialRate(float specialRate) {
		this.specialRate = specialRate;
	}
	
	@Override
	public float calculatePrice() {
		float totalPrice = super.getRootprice() + super.calculateTax() + super.getRootprice() * this.specialRate;
		return totalPrice;
		
	}
	
	public float calculatePrice(float transportCost) {
		float totalPrice = super.getRootprice() + super.calculateTax() + super.getRootprice() * this.specialRate + transportCost;
		return totalPrice;
	}
	
	@Override
	public void setInfor() {
		Scanner input = new Scanner(System.in);
		super.setInfor();
		while(true) {
			try {
				System.out.println("Special Rate: ");
				this.specialRate = Float.parseFloat(input.nextLine());
				break;
			}catch(NumberFormatException e) {
				System.out.println("Invalid Special Rate");
			}
			
		}
	}
}
-------------------------------------------------------------------------------------
package Lesson_30_3_2020;

public class Test {
	public static void main(String[] args) {
		LuxuryCar myLuxuryCar = new LuxuryCar();
		myLuxuryCar.setInfor();
		myLuxuryCar.getInfor();
		System.out.println("Total price with transportcost: " + myLuxuryCar.calculatePrice(20000) + "$");
	}
}



avatar
Nguyễn Hoàng Anh [C1907L]
2020-03-31 10:03:55



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

/**
 *
 * @author Redmibook 14
 */
interface  Icar {
    public float calculateTax();
    public float calculatePrice();
    public void getInfo();
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

/**
 *
 * @author Redmibook 14
 */
public class Car implements Icar {

    private String name, producer;
    
    private int seat,year;
    private float rootprice;

    public void setName(String name) {
        this.name = name;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public void setRootprice(float rootprice) {
        this.rootprice = rootprice;
    }

    public String getName() {
        return name;
    }

    public String getProducer() {
        return producer;
    }

    public int getYear() {
        return year;
    }

    public int getSeat() {
        return seat;
    }

    public float getRootprice() {
        return rootprice;
    }

    @Override
    public float calculateTax() {
        float tax;
        if (this.seat < 7) {
            tax = (this.rootprice * 60) / 100;
        } else {
            tax = (this.rootprice * 70) / 100;
        }
        return tax;
    }

    @Override
    public float calculatePrice() {
        float TotalPrice = this.rootprice + calculateTax();
        return TotalPrice;
    }

    @Override
    public void getInfo() {
        System.out.println(name + " car produced by " + producer + " in " + year + " has " + seat + " seats with the total price is " + calculatePrice() + "$.");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

/**
 *
 * @author Redmibook 14
 */
public class LuxuryCar extends Car {

    private float specialRate;

    @Override
    public float calculatePrice() {
        float TotalPrice = getRootprice() + calculateTax() + getRootprice()
                * specialRate;
        return TotalPrice;
    }

    public float calculatePrice(float transportCost) {
        float TotalPrice = getRootprice()
                + calculateTax() + getRootprice() * specialRate + transportCost;
        return TotalPrice;

    }

    @Override
    public void getInfo() {
        System.out.println(getName() + " car produced by " + getProducer() + " in " + getYear() + " has " + getSeat() + " seats with the total price is " + calculatePrice() + "$.");
    }

    public void getInfo(float transportCost) {
        System.out.println(getName() + " car produced by " + getProducer() + " in " + getYear() + " has " + getSeat() + " seats with the total price is " + calculatePrice(transportCost) + "$.");
    }
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package CarManager;

import java.util.*;

/**
 *
 * @author Redmibook 14
 */
public class Test {

    public static void main(String[] args) {
        LuxuryCar myLuxCar = new LuxuryCar();
        Scanner input = new Scanner(System.in);
        System.out.println("1.Enter name : ");
        String name = input.nextLine();
        myLuxCar.setName(name);
        System.out.println("2.Enter Producer : ");
        String producer = input.nextLine();
        myLuxCar.setProducer(producer);
        System.out.println("3.Enter Year : ");
        while (true) {
            try {
                int year = Integer.parseInt(input.nextLine());
                myLuxCar.setYear(year);
                System.out.println("4.Enter Seat : ");
                int seat = Integer.parseInt(input.nextLine());
                myLuxCar.setSeat(seat);
                System.out.println("5.Enter RootPrice  : ");
                float RootPrice = Float.parseFloat(input.nextLine());
                myLuxCar.setRootprice(RootPrice);
                myLuxCar.getInfo();
                System.out.println("in case transportCost is 20,000$  : ");
                myLuxCar.getInfo(20000);
            } catch (NumberFormatException e) {
                 System.err.println("INVALID INPUT");
            }
        }
    }
}


avatar
Ngô Quang Huy [C1907L]
2020-03-30 15:45:22



package March30;

public interface Icar {
    float calculateTax();
    float calculatePrice();
    void getInfor();
}



package March30;


import java.util.Scanner;


public class Car implements Icar{
    public String name,producer;
    public int year,seat;
    public float tax,TotalPrice,rootprice;
    
    public Car() {
    }

    public Car(String name, String producer, int year, int seat, int rootprice) {
        this.name = name;
        this.producer = producer;
        this.year = year;
        this.seat = seat;
        this.rootprice = rootprice;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getProducer() {
        return producer;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getSeat() {
        return seat;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public float getRootprice() {
        return rootprice;
    }

    public void setRootprice(int rootprice) {
        this.rootprice = rootprice;
    }

    public float getTax() {
        return tax;
    }

    public void setTax(float tax) {
        this.tax = tax;
    }

    public float getTotalPrice() {
        return TotalPrice;
    }

    public void setTotalPrice(float TotalPrice) {
        this.TotalPrice = TotalPrice;
    }

    @Override
    public float calculateTax() {
        if(seat<7){
            this.tax = this.rootprice*60/100;
        }else{
            this.tax = this.rootprice*70/100;
        }
        return this.tax;
    }

    @Override
    public float calculatePrice() {
        this.TotalPrice = rootprice + tax;
        return this.TotalPrice;
    }

    @Override
    public void getInfor() {
        System.out.println(name+" car produced by "+ producer +" in "
                +year+" has "+seat+" seats with the total price is "
                +TotalPrice+"$");
    }

    void inputCar(){
        Scanner input = new Scanner(System.in);
        System.out.print("Insert Name: ");
        name=input.nextLine();
        System.out.print("Insert Producer: ");
        producer=input.nextLine();
        while(true){
            try{
                System.out.print("Insert Year: ");
                year=Integer.parseInt(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
        while(true){
            try{
                System.out.print("Insert Seat: ");
                seat=Integer.parseInt(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
        while(true){
            try{
                System.out.print("Insert rootPrice : ");
                rootprice=Float.parseFloat(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
    }
}



package March30;

import java.util.Scanner;

public class LuxuryCar extends Car{
    private float specialRate;

    public LuxuryCar(float specialRate) {
        this.specialRate = specialRate;
    }

    public LuxuryCar(float specialRate, String name, String producer, int year, int seat, int rootprice) {
        super(name, producer, year, seat, rootprice);
        this.specialRate = specialRate;
    }

    public LuxuryCar() {
    }

    @Override
    public float calculatePrice() {
        super.TotalPrice = super.rootprice + super.tax + super.rootprice*specialRate;
        return super.TotalPrice;
    }
    
    public float calculatePrice(float transportCost) {
        super.TotalPrice = super.rootprice + super.tax + super.rootprice*this.specialRate + transportCost;
        return super.TotalPrice;
    }

    @Override
    void inputCar() {
        super.inputCar();
        Scanner input = new Scanner(System.in);
        while(true){
            try{
                System.out.print("Insert specialRate: ");
                this.specialRate=Float.parseFloat(input.nextLine());
                break;
            }catch(NumberFormatException e){
                System.err.println("INVALID INPUT");
            }
        }
        
    }
    
    
    
}



package March30;

public class Test {
    public static void main(String[] args) {
        LuxuryCar car = new LuxuryCar();
        car.inputCar();
        car.calculateTax();
        car.calculatePrice(20000);
        System.out.println("\nCar information:");
        car.getInfor();
        System.out.println("\nTotal Price is "+car.TotalPrice);
    }
}


avatar
trung [C1907L]
2020-03-30 15:20:22



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

/**
 *
 * @author prdox
 */
interface Icar {
    //calculate tax of the car
    public float calculateTax();
    //Calculate the total cost of the car
    public float calculatePrice();
    //Display the information of the car
    public void getInfor();
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Car implements Icar {

    String name, producer;
    int year, seat;
    float rootprice;

    public Car() {
    }

    public Car(String name, String producer, int year, int seat, float rootprice) {
        this.name = name;
        this.producer = producer;
        this.year = year;
        this.seat = seat;
        this.rootprice = rootprice;
    }

    public void inputInfo() {
        Scanner input = new Scanner(System.in);
        System.out.println("Nhap vao ten xe");
        name = input.nextLine();
        System.out.println("Nhap vao hang san xuat");
        producer = input.nextLine();
        while(true) {
            try {
                System.out.println("Nhap vao nam san xuat");
                year = Integer.parseInt(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }
        while(true) {
            try {
                System.out.println("Nhap vao so ghe");
                seat = Integer.parseInt(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }
        while(true) {
            try {
                System.out.println("Nhap vao gia goc");
                rootprice = Float.parseFloat(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }

    }

    public String getName() {
        return name;
    }

    public String getProducer() {
        return producer;
    }

    public int getYear() {
        return year;
    }

    public int getSeat() {
        return seat;
    }

    public float getRootprice() {
        return rootprice;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setProducer(String producer) {
        this.producer = producer;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }

    public void setRootprice(int rootprice) {
        this.rootprice = rootprice;
    }

    @Override
    public float calculateTax() {
        if (seat < 7) {
            return 0.6f * rootprice;
        }
        return 0.7f * rootprice;
    }

    @Override
    public float calculatePrice() {
        return rootprice + calculateTax();
    }

    @Override
    public void getInfor() {
        System.out.format("%s car produced by %s in %d has %d seats with the total price is %f$", name, producer, year, seat, calculatePrice());
    }

}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class LuxuryCar extends Car{
    private float specialRate;

    public LuxuryCar() {
    }

    public LuxuryCar(String name, String producer, int year, int seat, float rootprice,float specialRate) {
        super(name,producer,year,seat,rootprice);
        this.specialRate = specialRate;
    }

    public float getSpecialRate() {
        return specialRate;
    }
    
    @Override
    public void inputInfo() {
        Scanner input = new Scanner(System.in);
        super.inputInfo();
        while(true) {
            try {
                System.out.println("Nhap vao rate");
                specialRate = Float.parseFloat(input.nextLine());
                break;
            } catch (NumberFormatException e) {
                System.out.println("Invalid input!");
            }
        }
    }
    
    @Override
    public float calculatePrice(){
        return rootprice + calculateTax() + rootprice*specialRate ;
    }
    
    public float calculatePrice(float transportCost){
        return calculatePrice() + transportCost;
    }
    
    
}



/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Buoi9;

import java.util.Scanner;

/**
 *
 * @author prdox
 */
public class Test {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        
        LuxuryCar myLuxuryCar = new LuxuryCar();
        myLuxuryCar.inputInfo();
        myLuxuryCar.getInfor();
        System.out.println("\nTotal price is "+myLuxuryCar.calculatePrice(20000f));

    }
}