Computer Science
Southern New Hampshire University Method Driving Cost Java Code Project

Southern New Hampshire University

Question Description

Can you help me understand this Java question?

I am doing the questions in a website, so I have to try the answer and it will tell me if its correct. Please be patience with me as you know those website does not take answer with minor errors.

Question 1:

Write a method drivingCost() with input parameters drivenMiles, milesPerGallon, and dollarsPerGallon, that returns the dollar cost to drive those miles. All items are of type double. If the method is called with 50 20.0 3.1599, the method returns 7.89975.

Define that method in a program whose inputs are the car's miles/gallon and the gas dollars/gallon (both doubles). Output the gas cost for 10 miles, 50 miles, and 400 miles, by calling your drivingCost() method three times.

Output each floating-point value with two digits after the decimal point, which can be achieved as follows:
`System.out.printf("%.2f", yourValue);`

The output ends with a newline.

Ex: If the input is:

``````20.0 3.1599
``````

the output is:

``````1.58 7.90 63.20
``````

Your program must define and call a method:
`public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon`

Question 2:

This program outputs a downwards facing arrow composed of a rectangle and a right triangle. The arrow dimensions are defined by user specified arrow base height, arrow base width, and arrow head width.

(1) Modify the given program to use a loop to output an arrow base of height arrowBaseHeight. (1 pt)

(2) Modify the given program to use a loop to output an arrow base of width arrowBaseWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow base. (1 pt)

(3) Modify the given program to use a loop to output an arrow head of width arrowHeadWidth. Use a nested loop in which the inner loop draws the *’s, and the outer loop iterates a number of times equal to the height of the arrow head. (2 pts)

(4) Modify the given program to only accept an arrow head width that is larger than the arrow base width. Use a loop to continue prompting the user for an arrow head width until the value is larger than the arrow base width. (1 pt)

``````while (arrowHeadWidth <= arrowBaseWidth) {
}
``````

Example output for arrowBaseHeight = 5, arrowBaseWidth = 2, and arrowHeadWidth = 4:

``````Enter arrow base height: 5
Enter arrow base width: 2
``````
``````**
**
**
**
**
****
***
**
*``````

Unformatted Attachment Preview

// Question 1 import java.util.Scanner; public class LabProgram { public static double drivingCost(double drivenMiles, double milesPerGallon, double dollarsPerGallon) { double totalCost = (drivenMiles / milesPerGallon) * dollarsPerGallon; return totalCost; } /* Define your method here */ } public static void main(String[] args) { /* Type your code here. */ double milesG; double dollarsG; Scanner scnr = new Scanner(System.in); dollarsG = scnr.nextDouble(); drivingCost(10.0, milesG, dollarsG); milesG = scnr.nextDouble(); dollarsG = scnr.nextDouble(); drivingCost(50.0, milesG, dollarsG); milesG = scnr.nextDouble(); dollarsG = scnr.nextDouble(); drivingCost(400.0, milesG, dollarsG); } // Question 2 import java.util.Scanner; public class DrawHalfArrow { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); int arrowBaseHeight = 0; int arrowBaseWidth = 0; int arrowHeadWidth = 0; System.out.println("Enter arrow base height: "); arrowBaseHeight = scnr.nextInt(); System.out.println("Enter arrow base width: "); arrowBaseWidth = scnr.nextInt(); System.out.println("Enter arrow head width: "); arrowHeadWidth = scnr.nextInt(); // Draw arrow base (height = 3, width = 2) System.out.println("**"); System.out.println("**"); System.out.println("**"); // Draw arrow head (width = 4) System.out.println("****"); System.out.println("***"); System.out.println("**"); System.out.println("*"); return; } } ...
Purchase answer to see full attachment
Student has agreed that all tutoring, explanations, and answers provided by the tutor will be used to help in the learning process and in accordance with Studypool's honor code & terms of service.

At...

Tellos (1663)
Carnegie Mellon University
Review
Review

Anonymous
Top quality work from this tutor! I’ll be back!

Anonymous
Just what I needed… fantastic!

Anonymous
Use Studypool every time I am stuck with an assignment I need guidance.

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4