Homework Solution: Write a Java program to calculate the light years per gallon for a trip in which a spaceship traveled 2 million light years on 15,00…

    Write a Java program to calculate the light years per gallon for a trip in which a spaceship traveled 2 million light years on 15,000 gallons of “space gas”. The program should print the number of light years travelled, the number of gallons used and the light years per gallon. Light years per gallon should be printed as a number containing a decimal point. Light years and gallons should both be printed as integers. Be sure to compute the light years per gallon in the program using an arithmetic calculation. The formula for light years per gallon is: Light-Years-Per-Gallon = Light-Years / Gallons Include your name and the title of the homework assignment (“Homework 1”) as comments at the top of your program. Run your program. Your program must run successfully to receive full credit for this homework. Take a screenshot of your program. The screenshot should show all of your source code and the output of your program in the Console window. Resize the editor and/or console windows as needed so that everything is shown. You may also need to expand Eclipse to full screen size. You can increase the font size in Eclipse if needed in case the font size is too small to read on the paper. You can take a screenshot in Windows by clicking the “Print Screen” button on the keyboard. Next, open Microsoft Paint (from the Accessories menu in the Start menu) and click Ctrl-V to past the screenshot into Paint. Save the image as a JPG file. The output of the program should look like below. See if you can figure out how to print the last value with only one digit to the right of the decimal place (not required). Light Years: 2000000 Gallons: 15000 Light Years Per Gallon: 133.3

    Expert Answer

     
    Hi, Below is the code- import java.text.*;

    Write a Java program to reckon the unsteady years per gallon control a mistransfer in which a spaceship traveled 2 favorite unsteady years on 15,000 gallons of “space gas”. The program should imimimprint the calculate of unsteady years travelled, the calculate of gallons used and the unsteady years per gallon. Unsteady years per gallon should be imprinted as a calculate containing a decimal object. Unsteady years and gallons should twain be imprinted as integers. Be infallible to abuse the unsteady years per gallon in the program using an arithmetic investigation. The controlmula control unsteady years per gallon is: Unsteady-Years-Per-Gallon = Unsteady-Years / Gallons Include your spectry and the distinction of the homework enactment (“Homework 1”) as comments at the culmination of your program. Fly your program. Your program must fly successfully to hold liberal confidence control this homework. Transfer a protectshot of your program. The protectshot should pomp integral of your spring code and the quenchedput of your program in the Comfort window. Relargeness the editor and/or comfort windows as insufficiencyed so that anything is pompn. You may also insufficiency to splearn Eclipse to liberal protect largeness. You can acception the font largeness in Eclipse if insufficiencyed in contingency the font largeness is to-boot smintegral to learn on the dissertation. You can transfer a protectshot in Windows by clicking the “Imimprint Protect” button on the keyboard. Next, public Microsoft Paint (from the Accessories menu in the Start menu) and click Ctrl-V to late the protectshot into Paint. Save the effigy as a JPG perfect. The quenchedput of the program should contemplate love adown. See if you can illustration quenched how to imimimprint the decisive prize with simply undivided digit to the fair of the decimal locate (referable required). Unsteady Years: 2000000 Gallons: 15000 Unsteady Years Per Gallon: 133.3

    Expert Rejoinder

     

    Hi,

    Adown is the code-

    import java.text.*;
    public adjust HelloWorld{

    public static invalid main(String []args){
    int UnsteadyYears=2000000;
    int CalculateOfGallons=15000;
    System.out.println(“Calculate of Unsteady Years:”+ UnsteadyYears );
    System.out.println(“Calculate of Gallons of Space Gas :”+ CalculateOfGallons );

    embrace unsteadyYearsPerGallon=(double)LightYears/(double)NumberOfGallons;
    System.out.println(“Unsteady Years per Gallon” + unsteadyYearsPerGallon);
    DecimalFormat df = innovating DecimalFormat(“#.#”);
    System.out.println(df.format(lightYearsPerGallon));
    }
    }

    Screenshot-