In: Computer Science
JAVA ONLY.
Program 3: Distance calc. This question is fairly straightforward. Design (pseudocode) and implement (source code) a program to compute the distance between 2 points. The program prompts the user to enter 2 points (X1, Y1) and (X2, Y2). The distance between 2 points formula is: Square_Root [(X2 – X1)^2 + (Y2 – Y1)^2] Document your code, properly label the input prompts, and organize the outputs as shown in the following sample runs. Note: for C++, #include and then call sqrt(). For Java, you’ll use Math.sqrt() and for C# it’s Math.Sqrt(). See the short appendix below for the general form. Sample run 1: Entered X1: 1.5 Entered Y1: -3.4 Entered X2: 4 Entered Y2: 5 Distance: 8.764131445842194 Sample run 2: Entered X1: -5.5 Entered Y1: -8.8 Entered X2: 25 Entered Y2: 4.5 Distance: 33.27371334852784 Sample run 3: Entered X1: 2 Entered Y1: 2 Entered X2: 10 Entered Y2: 10 Distance: 11.313708498984761
Program 4: Is there a Prius version? Did you know that the average Boeing 747 airplane uses approximately 1 gallon of fuel per second? Given the speed of the airplane, that means it gets 5 gallons to the mile. No, not 5 miles to the gallon, 5 gallons to the mile. You may be questioning why such a horribly inefficient machine is allowed to exist, but you’ll be happy to find out that, because this airplane hold 568 people, it averages about 0.01 gallons per person – (100 miles per gallon per person). Your job is to design (pseudocode) and implement (source code) a program that asks the user for a distance the plane has to fly (i.e. the length of the trip) and also asks the cost of jet fuel (¬¬which is currently $1.80 per gallon). The program should then calculate the total fuel charges to make the trip. Next, ask the user how many people will fly, as well as the average cost of a ticket. Finally, print the total profit made (or lost) and the average gas mileage per person. Document your code and properly label the input prompts and the outputs as shown below. Sample run 1: Enter the flight distance: 1000 Enter the current cost of jet fuel: $2 The flight will cost $10000.0 in fuel. Enter the number of passengers: 5 Enter the average cost of a ticket: 1000 You will make a profit of $-5000.0 You averaged 1.0 miles per person per gallon! Sample run 2: Enter the flight distance: 500 Enter the current cost of jet fuel: $3 The flight will cost $7500.0 in fuel. Enter the number of passengers: 500 Enter the average cost of a ticket: 300 You will make a profit of $142500.0 You averaged 100.0 miles per person per gallon! Sample run 3: Enter the flight distance: 4200 Enter the current cost of jet fuel: $1.80 The flight will cost $37800.0 in fuel. Enter the number of passengers: 550 Enter the average cost of a ticket: 600 You will make a profit of $292200.0 You averaged 110.0 miles per person per gallon!
Thanks for the question. Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks =========================================================================== import java.util.Scanner; public class ProblemThree { public static void main(String[] args) { // scanner object to accept user input Scanner scanner = new Scanner(System.in); // create four variables double x1, y1, x2, y2; // get x1 and y1 from user System.out.print("Enter X1: "); x1 = scanner.nextDouble(); System.out.print("Enter Y1: "); y1 = scanner.nextDouble(); // get x2 and y2 from user System.out.print("Enter X2: "); x2 = scanner.nextDouble(); System.out.print("Enter Y2: "); y2 = scanner.nextDouble(); //calculate distance using Math.sqrt() method double distance = Math.sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2)); //display distance System.out.println("Distance: "+distance); } }
===========================================================================
===========================================================================
import java.util.Scanner; public class ProblemFour { private static final double GALLON_PER_SECOND=5.0; public static void main(String[] args) { // scanner object to accept user input Scanner scanner = new Scanner(System.in); double flightDistance; double fuelCost; int passengersInFlight=0; double averageTicketCost =0; // take user inputs System.out.print("Enter the flight distance: "); flightDistance=scanner.nextDouble(); System.out.print("Enter the current cost of jet fuel: $"); fuelCost = scanner.nextDouble(); double flightCost = flightDistance*fuelCost*GALLON_PER_SECOND; System.out.println("The flight will cost $"+String.format("%.1f",flightCost)+" in fuel"); System.out.print("Enter the number of passengers: "); passengersInFlight = scanner.nextInt(); System.out.print("Enter the average cost of a ticket: "); averageTicketCost = scanner.nextDouble(); // calculate profit Selling Price - Cost Price double profit = passengersInFlight*averageTicketCost-flightCost; System.out.println("You will make a profit of $"+String.format("%.1f",profit)); // calculate average miles per person double averageMilesPerPerson = passengersInFlight/GALLON_PER_SECOND; System.out.println("You averaged "+String.format("%.1f",averageMilesPerPerson) + " miles per person per gallon!"); } }
===========================================================================
thanks a lot !
let me know for any questions or doubts & please do give a up vote : )