Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,153,492 members, 7,819,797 topics. Date: Monday, 06 May 2024 at 11:43 PM

Solutions To Java How To Program By Deitel, 9th Edition - Programming - Nairaland

Nairaland Forum / Science/Technology / Programming / Solutions To Java How To Program By Deitel, 9th Edition (2076 Views)

How To Program Outseal Arduino PLC / First Thing First, Learn To Program! / Introduction To Java (2) (3) (4)

(1) (Reply) (Go Down)

Solutions To Java How To Program By Deitel, 9th Edition by babajeje123(m): 12:37pm On Sep 02, 2017
I started learning java some months back and hmmm, it's been fun and challenging. I am strictly using Deitel because I don't want confusion from different authors. I'm currently on chapter 4 and I've able to provide solutions to most of the practice questions.

I am bringing my solutions here, not to boast, but to firstly encourage newbies and also to be constructively criticized.
Re: Solutions To Java How To Program By Deitel, 9th Edition by babajeje123(m): 12:41pm On Sep 02, 2017
/*
(GasMileage)Drivers are concerned with the mileage their automobiles get.One driver has kept track of several trips by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.

*/
import java.util.Scanner;

public class GasMileage
{
public static void main(String[]args)
{
Scanner input = new Scanner(System.in);


int mileage;
int gallon;
int totalMileage = 0;
int totalGallon = 0;
int counter = 1;
double milespergallonsused = 0;
double average;

System.out.print("Enter a mileage (or -1 to quit): "wink;
mileage = input.nextInt();
System.out.print("Enter the gallons of fuel used: "wink;
gallon = input.nextInt();
average = (double) mileage / gallon;
System.out.printf("The miles per gallons used for this trip is: %.2f", average);
System.out.println();


while (mileage != -1 && gallon != -1)
{
totalMileage = totalMileage + mileage;
totalGallon = totalGallon + gallon;
counter = counter + 1;
System.out.print("Enter a mileage (or -1 to quit): "wink;
mileage = input.nextInt();

System.out.print("Enter the gallons of fuel used: "wink;
gallon = input.nextInt();
average = (double) mileage / gallon;
System.out.printf("The miles per gallons used for this trip is: %.2f", average);
System.out.println();
}
if (counter != 0)
{
milespergallonsused = (double) totalMileage/totalGallon;

System.out.printf("The combined miles per gallons used for all the trips is: %.2f", milespergallonsused);
}
}
}
Re: Solutions To Java How To Program By Deitel, 9th Edition by babajeje123(m): 12:45pm On Sep 02, 2017
I really don't know why the question didn't display in the comment section of the code. But here is it...
(GasMileage)Drivers are concerned with the mileage their automobiles get.One driver has kept track of several trips by recording the miles driven and gallons used for each tankful. Develop a Java application that will input the miles driven and gallons used (both as integers) for each trip. The program should calculate and display the miles per gallon obtained for each trip and print the combined miles per gallon obtained for all trips up to this point. All averaging calculations should produce floating-point results. Use class Scanner and sentinel-controlled repetition to obtain the data from the user.
Re: Solutions To Java How To Program By Deitel, 9th Edition by chichijas(m): 12:47pm On Sep 02, 2017
oboy i am new in programming! what you are reading is it e-book or just portable book?
Re: Solutions To Java How To Program By Deitel, 9th Edition by babajeje123(m): 2:01pm On Sep 03, 2017
I don't know what I did here that warranted me a ban. I will still continue
Re: Solutions To Java How To Program By Deitel, 9th Edition by CodeBlooded: 10:16pm On Sep 03, 2017
Why not zip it and drop in ur google drive or github and give out the link.
Re: Solutions To Java How To Program By Deitel, 9th Edition by babajeje123(m): 12:19pm On Sep 04, 2017
CodeBlooded:
Why not zip it and drop in ur google drive or github and give out the link.

ok. Thanks

(1) (Reply)

Consuming Laravel Api In Android / [Help Request] Teach Me How To Design Databases. / What PHP Framework Is The Best?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 14
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.