Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- //Program that will work out the cost of running a car with given distances
- public class car{
- public static double Calculation(double[] carOne,double distance){
- double timesForDistance=Math.floor(distance/carOne[2]);
- double timesToFill=distance/carOne[3];
- double total=carOne[0]+(timesForDistance*carOne[1])+(timesToFill*carOne[4]);
- return total;
- }
- public static double[] newCar(){
- Scanner in= new Scanner(System.in);
- double[] carNew= new double[5];
- //asks for price of car
- System.out.print("\nPlease enter the price of the car: ");
- carNew[0]=in.nextDouble();
- //ask for price to service
- System.out.print("\nPlease enter the price to service the car: ");
- carNew[1]=in.nextDouble();
- //asks for the service interval
- System.out.print("\nPlease enter the interval between services: ");
- carNew[2]=in.nextDouble();
- //asks for km/l
- System.out.print("\nPlease enter the km/l: ");
- carNew[3]=in.nextDouble();
- //ask for fuel cost per litre
- System.out.print("\nPlease enter the fuel cost per litre: ");
- carNew[4]=in.nextDouble();
- return carNew;
- }
- public static double[] valuesForDistances(){
- Scanner in=new Scanner(System.in);
- double[] valForDis=new double[3];
- System.out.print("\nEnter the min distance: ");
- valForDis[0]=in.nextDouble();
- System.out.print("\nEnter the max distance: ");
- valForDis[1]=in.nextDouble();
- System.out.print("\nEnter the interval: ");
- valForDis[2]=in.nextDouble();
- return valForDis;
- }
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- int choice=0;
- //menu system to work out what the user wants to do
- while (choice<1||choice>3){
- System.out.print("\n Menu:\n1)For one distance\n2)For range of distance\n3)For 2 cars over range\n");
- choice=in.nextInt();
- }
- //will work out the price to run the car for a set distance
- if (choice==1){
- double[] carOne;
- carOne=car.newCar();
- System.out.print("\nEnter the distance the car is going to do: ");
- double distance=in.nextDouble();
- System.out.printf("The cost of the car: \u00A3%1.2f",car.Calculation(carOne, distance));
- }
- //will work out the price to run on car over range of distance
- if (choice==2){
- double[] carOne;
- double[] val;
- val=car.valuesForDistances();
- carOne=car.newCar();
- for (double counter=val[0]; counter<val[1]; counter+=val[2])
- {
- System.out.printf("\nThe cost of the car for distance %.2f is: \u00A3%1.2f" ,counter ,car.Calculation(carOne, counter));
- }
- }
- //will make a table comparing distances and price for two cars
- if (choice==3){
- double[] carOne;
- double[] carTwo;
- double[] val;
- val=car.valuesForDistances();
- System.out.print("\nCAR ONE\n");
- carOne=car.newCar();
- System.out.print("\nCAR TWO\n");
- carTwo=car.newCar();
- for (double counter=val[0]; counter<val[1]; counter+=val[2])
- {
- System.out.printf("\nMiles: %f1.2: Car one cost: \u00A3%1.2f Car two cost: \u00A3%1.2f",counter,car.Calculation(carOne,counter),car.Calculation(carTwo,counter));
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement