Calculating price per square inch of a pizza in Java - java

I am writing a java program to calculate the area of of a pizza in one method and to calculate the price per square inch of a pizza in another method. I have the area method working, but am not getting any output when I try to calculate the price per square inch. I think it has something to do with calling the area() method in the ppsi method, but I'm not sure exactly what I'm doing wrong or how to correctly call the area method.
import java.util.Scanner;
public class Pizza {
public static void main(String[] args){
System.out.println("What is the size of your pizza in inches?");
System.out.println(area() + " square inches");
System.out.println("What is the price of your pizza?");
System.out.println(ppsi());
}
public static double area(){
Scanner keyboard = new Scanner(System.in);
double diameter = keyboard.nextDouble();
return (diameter / 2) * (diameter / 2) * Math.PI;
}
public static double ppsi(){
Scanner keyboard = new Scanner(System.in);
double price = keyboard.nextDouble();
return((area()) / price);
}
}

In your ppsi method, it calls area, which is going to prompt the user to enter ANOTHER value in AGAIN. Instead, prompt for these values first, then pass them into your method
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the size of your pizza in inches?");
double diameter = keyboard.nextDouble();
System.out.println(area(diameter) + " square inches");
double price = keyboard.nextDouble();
System.out.println("What is the price of your pizza?");
System.out.println(ppsi(diameter, price));
}
public static double area(double diameter){
//Scanner keyboard = new Scanner(System.in);
//double diameter = keyboard.nextDouble();
return (diameter / 2) * (diameter / 2) * Math.PI;
}
public static double ppsi(double diameter, double price){
//Scanner keyboard = new Scanner(System.in);
//double price = keyboard.nextDouble();
return((area(diameter)) / price);
}
This way, the methods are doing ONE job, not two

You are calling area again in ppsi method , why does you need it again to read?
I think you want some thing like below , i just modified your code:
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
System.out.println("What is the size of your pizza in inches?");
double inches=scanner.nextDouble();
double area=area(inches);
System.out.println(area + " square inches");
System.out.println("What is the price of your pizza?");
double price=scanner.nextDouble();
System.out.println(ppsi(area, price));
}
public static double area(double diameter){
return (diameter / 2) * (diameter / 2) * Math.PI;
}
public static double ppsi(double area, double price){
return((area) / price);
}

After you print the prompt for "what is the price..." Your program is expecting the user to enter a price, and then enter area again. If you enter two numbers after that prompt, I expect you'll see some output.

Related

how to enable my code to skip over certain code when the input by the user is a specific option

Hi I was wondering if someone could help me make it so when I choose an option in my code when the program runs, it skips all the other options that weren't chosen and only uses the code inputted by the user. So only the specific parts in the code should be printed but I don't know how to skip over code. There is probably things I should remove and then things I should add but I need help.
package test;
import java.util.Scanner;
import java.text.NumberFormat;
public class Test
{
static Scanner input = new Scanner( System.in );
public static void main(String[] args)
{
//Variables
String Option;
int a;
int Base;
int Height;
int Length;
int Width;
int Radius;
int r;
int b;
double Area;
//Menu
System.out.println("Welcome to the Area Calculator");
System.out.println("");
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.out.println("3. Circle");
System.out.println("4. Triangle");
System.out.println("5. Quit");
System.out.println("Please enter the number of the shape you would like to calculate the area for.");
Option=input.next();
}
public static void Sqaure(String[] args)
{
System.out.println("Please enter the length of one side.");
int a=input.nextInt();
System.out.println("Please enter the length of one side.");
double area = a*a;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Rectangle(String[] args)
{
System.out.println("Please enter the length.");
int Length=input.nextInt();
System.out.println("Please enter the width.");
int Width=input.nextInt();
double area = Length*Width;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Circle(String[] args)
{
System.out.println("Please enter the radius.");
int r=input.nextInt();
double Radius = r*r;
double area = Math.PI * Radius;
System.out.println("The area of the given shape is " + area + " square units.");
}
{
System.out.println("Please enter the base length.");
int b=input.nextInt();
double Base = b * .5;
System.out.println("Please enter the height lenth.");
int Height=input.nextInt();
double area = Base * Height;
System.out.println("The area of the given shape is " + area + " square units.");
}
}
So, it is clear to me you're very new with Java and you're learning! So I went ahead and made a complete working version of what you are trying to do.
Although it is a simple program Ill go through what each section does.
Prompt Method
Asks the user which area they want to find, this method has the direct answer to your question in the form of a series of if/if else statements. It gets the input from the user and then goes through the series of if/if else statements, seeing if the value entered matches any of them, if it does then it runs that section of code and skips the rest. If a number other then 1-4 is entered, it quits the program (see the else statement), see this page for more info on if statements. I opted for a bunch of if statements but there is a better, cleaner way, but it is a little confusing if you are brand new, see here to learn about those.
Area Methods
These are all pretty much the same except for he way they calculate the areas. They first prompt the user for the required measurements for that shape and assign those entered values to variables, then it prints out the resulting area after running the values through a calculation, then the program stops.
Main Method
All the main method is doing is calling prompt, that keeps it clean, you don't really ever want to do anything but call other methods or create objects in the main method, so most people put the nitty gritty somewhere else and call it.
Below is the working code
Go ahead and paste into a doc and run it, just be aware that I did not bother to add any error catching to keep it simple so keep it to whole numbers without decimals (also called ints, eg. 1, 2, 4, not 4.3, 5.4, etc.)
Side note:
In your methods you have (String args[]) in the brackets, this is not needed, in fact it will cause trouble, that is only used in the main method and is a relatively advanced way of providing input to the program, for now you can keep to leaving the brackets empty.
import java.util.*;
public class Area
{
public static void main(String args[])
{
prompt(); //Calls prompt method
}
//Prompt Method
public static void prompt()
{
int option;
Scanner input = new Scanner(System.in);
//Ask the user which shape they want to find the area of
System.out.println("Select from the following shapes to calulate the area:");
System.out.println("Rectangle -- 1\nCircle -- 2\nTriangle -- 3\nQuit -- 4\n");
System.out.print("Your option: ");
option = input.nextInt();
System.out.println();
//THIS IS THE PART THAT DIRECTLY PERTAINS TO YOUR QUESTION
if(option == 1)
{
rectangle();
}
else if(option == 2)
{
circle();
}
else if(option == 3)
{
triangle();
}
else
{
System.out.println("Program stopped by user"); //For stopping the program when they want to quit instead
System.exit(0);
}
}
//Rectangle Method
public static void rectangle()
{
double h;
double b;
Scanner input = new Scanner(System.in);
System.out.println("RECTANGLE");
System.out.print("Enter Height: ");
h = input.nextDouble();
System.out.print("Enter Base: ");
b = input.nextDouble();
System.out.print("\nArea of Rectangle = " + (h*b) + "\n");
}
//Circle Method
public static void circle()
{
double pi = 3.14;
double radius;
Scanner input = new Scanner(System.in);
System.out.println("CIRCLE");
System.out.print("Enter radius: ");
radius = input.nextDouble();
System.out.print("\nArea of Circle = " + (pi*(radius*radius)) + "\n");
}
//Triangle Method
public static void triangle()
{
double h;
double b;
Scanner input = new Scanner(System.in);
System.out.println("TRIANGLE");
System.out.print("Enter Height: ");
h = input.nextDouble();
System.out.print("Enter Base: ");
b = input.nextDouble();
System.out.print("\nArea of Triangle = " + ((h*b)/2) + "\n");
}
}
package test;
import java.util.Scanner;
import java.text.NumberFormat;
public class Test
{
static Scanner input = new Scanner( System.in );
public static void main(String[] args)
{
//Variables
String Option;
int a;
int Base;
int Height;
int Length;
int Width;
int Radius;
int r;
int b;
double Area;
String [] inputString = new String [50];
//Menu
System.out.println("Welcome to the Area Calculator");
System.out.println("");
System.out.println("1. Square");
System.out.println("2. Rectangle");
System.out.println("3. Circle");
System.out.println("4. Triangle");
System.out.println("5. Quit");
System.out.println("Please enter the number of the shape you would like to calculate the area for.");
Option=input.next();
switch (Option) {
case "1": Sqaure(inputString);
break;
case "2": Rectangle(inputString);
break;
case "3": Circle(inputString);
break;
default: someExtraFig(inputString);
break;
}
}
public static void Sqaure(String[] args)
{
System.out.println("Please enter the length of one side.");
int a=input.nextInt();
System.out.println("Please enter the length of one side.");
double area = a*a;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Rectangle(String[] args)
{
System.out.println("Please enter the length.");
int Length=input.nextInt();
System.out.println("Please enter the width.");
int Width=input.nextInt();
double area = Length*Width;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void Circle(String[] args)
{
System.out.println("Please enter the radius.");
int r=input.nextInt();
double Radius = r*r;
double area = Math.PI * Radius;
System.out.println("The area of the given shape is " + area + " square units.");
}
public static void someExtraFig(String ...s)
{
System.out.println("Please enter the base length.");
int b=input.nextInt();
double Base = b * .5;
System.out.println("Please enter the height lenth.");
int Height=input.nextInt();
double area = Base * Height;
System.out.println("The area of the given shape is " + area + " square units.");
}
}

Unknown Source in Java Eclipse

I keep getting this error message and I can't figure out whats going on. I keep getting the error message "Exception in thread "main" java.util.NoSuchElementException" and then is says that my scanners have an unknown source.
Any ideas what's going on?
package pizza;
import java.util.Scanner;
public class Pizza {
public static void main(String[] args) {
Double diameter;
Double radius;
Double cost;
Double area;
final Double costPerInch;
//Ask and enter diameter
System.out.println("What is the diameter?");
Scanner size = new Scanner(System.in);
diameter = size.nextDouble();
size.close();
radius = diameter / 2;
//Ask and enter price
System.out.println("What is the price of the pizza?");
Scanner price = new Scanner(System.in);
cost = price.nextDouble();
price.close();
//Calculate cost per inch
area = radius * Math.PI;
costPerInch = cost / area;
//Output results
System.out.println("The cost per inch of the pizza is" + costPerInch);
I compiled this and ran it. Here is you problem:
size.close();
Once that closes, it loses the scanner altogether. Comment out that line, and the code works.

How do I get the program statement arguments like heightm passed down to other methods?

import java.util.Scanner ;
public class CollinsHealthCalculator {
double ACTIVITY_FACTOR = 1.375;
public static void main (String[] args) {
newHealthCalcDescription ();
Scanner keyboard = new Scanner (System.in);
System.out.println ("What is your weight in pounds? ");
double weightlb = keyboard.nextDouble ();
System.out.println ("What is your height in inches? ");
double heightin = keyboard.nextDouble ();
System.out.println ("What is your age in years? ");
double ageYears = keyboard.nextDouble ();
double WEIGHT_KILOGRAMS = weightlb / 2.2;
double HEIGHT_METERS = heightin * .0254;
double weightkg = WEIGHT_KILOGRAMS;
double heightm = HEIGHT_METERS;
double computingBMI (BMI, weightkg, heightm);
maleBMR (heightm, weightkg, ageYears);
femaleBMR (heightm, weightkg, ageYears);
showResults (BMI, caloriesm, caloriesf);
public static newHealthCalcDescription () {
System.out.println("This calculator will determine your BMI "
+ "(Body Mass Index). While also it will determine the amount "
+ "of calories needed to maintain weight.");
}
//Computing the BMI
public static void computingBMI (double BMI, double weightkg, double heightm){
BMI = weightkg/(Math.pow(heightm, 2));
}
//Computing BMR for male and female
public static void maleBMR (double heightm, double weightkg, double ageYears) {
double HEIGHT_CENTIMETERS = heightm * 100;
double heightcm = HEIGHT_CENTIMETERS ;
double BMRForMales = 13.397 * weightkg + 4.799 * heightcm - 5.677 * ageYears + 88.362;
double caloriesm = Math.round(BMRForMales * 1.375);
}
public static void femaleBMR (double heightm, double weightkg, double ageYears) {
double HEIGHT_CENTIMETERS = heightm * 100;
double heightcm = HEIGHT_CENTIMETERS ;
double BMRForFemales = 9.247 * weightkg + 3.098 * heightcm - 4.330 * ageYears + 447.593;
double caloriesf = Math.round(BMRForFemales * 1.375);
}
public static void showResults (double BMI, double caloriesm, double caloriesf) {
//Show results
System.out.printf ("%nYour BMI is: %7.1f", BMI);
System.out.println ("A BMI between 18.5 to 24.9 is considered normal.");
System.out.println ();
System.out.println ("To maintain current weight:");
System.out.print ("Men need to eat " + caloriesm);
System.out.println (" calories per day.");
System.out.print ("Females need to eat " + caloriesf);
System.out.println (" calories per day.");
}
}
I'm trying to get the code to pass down statements but I'm new to programming and have no clue on how to go about getting method passed down to another method. I've tried researching everywhere but I've had little luck in finding any help. Please help so I can make my programm functional I'm excited to learn just need help.
You can try giving the variables the global scope(outside the method). You may learn about it here.
When you declare a variable inside a method (i.e. code block), it is local to that block. So you cannot use that variable in any other method. Here the best option for you to do is to declare the variable, i.e. like weightkg etc as class variables.
You can change the return type of the methods from void to double and store the returned result and send the results to other methods.
for eg.
public static double computingBMI (double BMI, double weightkg, double heightm){
return weightkg/(Math.pow(heightm, 2));
}

java returning and calling variables

I am new to java and I am trying to make this bmi calculator but I am having trouble returning and calling variables. I am sure that I am doing something very wrong but have been unable to figure out how to properly do this after searching the internet my guess is I do not know what I should be searching. I will post the code, I am getting 4 errors in my main that are as follows:
required: double,double,double,double
found: no arguments
reason: actual and formal argument lists differ in length
I am assuming that I have improperly set up my variables but could really use a bit of guidance. Thank you in advance.
import java.util.Scanner;
public class cs210 {
public double weight;
public double height;
public double bmi;
public double wcal;
public double mcal;
public double age;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
method1 ();
method2 ();
method3 ();
method4 ();
method5 ();
}
public static void method1 () {
System.out.println ("This program implements a Health Assistance Calculator ");
System.out.println ("Given a weight, height, and age, it will compute:\n");
System.out.println ("BMI - Body Mass Index");
System.out.println ("Calories needed per day to maintain weight");
}
public double method2 (double weight, double height, double wcal, double bmi) {
Scanner keyboard = new Scanner (System.in);
System.out.println ("Please enter your weight:");
weight = keyboard.nextDouble ();
System.out.println ("Press 1 if weight was entered in Kg \n Press 2 if weight was entered in Lbs");
double wunits = keyboard.nextDouble();
if (wunits == 1) {
System.out.println("Thank you");
} else if (wunits == 2){
weight = weight / 2.2;
System.out.println("Thank you");
}
else {
System.out.println ("Please try again");
return 0;
}
System.out.println("Please enter your height:");
height = keyboard.nextDouble ();
System.out.println ("Press 1 if height was entered in meters \n Press 2 if height was entered in inches");
int hunits = keyboard.nextInt();
if(hunits ==1) {
System.out.println("Thank you");
} else if (hunits == 2){
height = height / 0.0254;
}else {
System.out.println("Please try again");
return 0;
}
System.out.println("Please enter your age in years:");
age = keyboard.nextDouble ();
bmi = weight / Math.pow(height, height);
return ( bmi + age + height + weight);
}
public static double method3(double weight, double age, double height) {
double paf = 1.375;
double mcal;
mcal = (13.397 * weight + 4.799 * height + 5.677 * age + 88.362) * paf;
return mcal;
}
public static double method4(double weight, double age, double height, double paf){
double wcal;
wcal = (93247 * weight + 3.098 * height - 4.330 * age + 447.593) * paf;
return wcal;
}
public double method5(double bmi, double mcal, double wcal){
System.out.println("Your BMI is:" + bmi);
System.out.println("A BMI in the range of 18.5 to 24.9 is considered normal\n");
System.out.println("To maintain your current weight:");
System.out.println("Men need" + mcal + "per day");
System.out.println("Women need" + wcal + "per day");
return 0;
}
}
You define method2 like this:
public double method2 (double weight, double height, double wcal, double bmi) {
// ...
It has four parameters, all double, just like your error message said. Then you call it like this:
method2 ();
Without any parameters at all, again just like the error message said. Since you defined it with four parameters, every time you call it you need to do it with four parameters. The values you use as parameters will be the values that the variables weight, height, wcal and bmi gets inside the function, and if you don't have any parameters the computer will not know what values to use for those variables and therefore throw an error to complain. So you could, as an example, do it like this:
method2(34.9, 23.4, 23.5, 34.1); // Just picked four random numbers here.
But looking at the structure of your program, it looks like you don
t want to pass any values to the function at all (since you let the user enter the values inside the function). Then you could just get rid of the parameters, and declare the variables inside the function:
public double method2 () {
double weight, height, wcal, bmi;
// ...
Now the variables will be available inside method2, but not anywhere else. If you want to use the same values later in the other functions, you could instead of declaring them inside your function declare them in your class, and they will become available anywhere in your class, but not anywhere else.
You will have to fix the same issue with the parameters for method3, method4 and method5 as well.
You need to pass parameters when you call to methods. If you call to method
public double method2 (double weight, double height, double wcal, double bmi)
You need to call it to like this method2 (50, 2, 200, 25.5);
When you call in to your other methods such as method3, method4, method5 ; you have to give appropriate parameters to those. But when it comes to your method1. It will not expecting any parameters so you don't want to pass any parameter to that method.
I think this small document will help you to understand method and arguments.
https://docs.oracle.com/javase/tutorial/java/javaOO/arguments.html
Its better to add your BMI logic and method to separate class then within the main method create and object and to the rest of manipulation. Otherwise it will hard to maintain and update properties and when you do it in that way remove your static methods. and use proper names for each and every method.
This code will give compilation errors because you have called methods in wrong way and you have call method2 and method5 within static method.
in method2() you have given 4 parameter but you are not using even a single parameter because you are getting from user.
like your modified code is
import java.util.Scanner;
public class cs21`enter code here`0 {
public static double weight;
public static double height;
public static double bmi;
public double wcal;
public double mcal;
public static double age;
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
cs210 cs=new cs210();
method1 ();
double val=cs.method2 ();
double value1=cs.method3 (weight,age,height);
double value2=cs.method4 (weight,age,height);
cs.method5 (bmi,value1,value2);
}
public static void method1 () {
System.out.println ("This program implements a Health Assistance Calculator ");
System.out.println ("Given a weight, height, and age, it will compute:\n");
System.out.println ("BMI - Body Mass Index");
System.out.println ("Calories needed per day to maintain weight");
}
public double method2 () {
Scanner keyboard = new Scanner (System.in);
System.out.println ("Please enter your weight:");
weight = keyboard.nextDouble ();
System.out.println ("Press 1 if weight was entered in Kg \n Press 2 if weight was entered in Lbs");
double wunits = keyboard.nextDouble();
if (wunits == 1) {
System.out.println("Thank you");
} else if (wunits == 2){
weight = weight / 2.2;
System.out.println("Thank you");
}
else {
System.out.println ("Please try again");
return 0;
}
System.out.println("Please enter your height:");
height = keyboard.nextDouble ();
System.out.println ("Press 1 if height was entered in meters \n Press 2 if height was entered in inches");
int hunits = keyboard.nextInt();
if(hunits ==1) {
System.out.println("Thank you");
} else if (hunits == 2){
height = height / 0.0254;
}else {
System.out.println("Please try again");
return 0;
}
System.out.println("Please enter your age in years:");
age = keyboard.nextDouble ();
bmi = weight / Math.pow(height, height);
return ( bmi + age + height + weight);
}
public static double method3(double weight, double age, double height) {
double paf = 1.375;
double mcal;
mcal = (13.397 * weight + 4.799 * height + 5.677 * age + 88.362) * paf;
return mcal;
}
public static double method4(double weight, double age, double height){
double wcal;
double paf=1.375;
wcal = (93247 * weight + 3.098 * height - 4.330 * age + 447.593) * paf;
return wcal;
}
public void method5(double bmi, double mcal, double wcal){
System.out.println("Your BMI is:" + bmi);
System.out.println("A BMI in the range of 18.5 to 24.9 is considered normal\n");
System.out.println("To maintain your current weight:");
System.out.println("Men need" + mcal + "per day");
System.out.println("Women need" + wcal + "per day");
}
}
Actually to make your code work the way it is written you should:
make all the fields and methods static
remove parameters from all methods declarations
declare local variable paf in method4.
That being said the code in that form is quite ugly. You should think about the following improvements:
class name should start from capital letter
class name should be something meaningful (e.g. BcmCalculator)
fields should be private
method should have meaningful names ( printGreetings, readUsersAttributes, etc)
in main method you should create instance of the class and call its methods
paf should be a constant (ie field private static final double PAF = 1.375;).
There are further possible improvements, but this should be enough for the beginning.

Java: Returning Multiple Values to Main Method In Order To Compute Total

The code is below. The program runs a series of calculations based on data input by the user. My problem is that for the most important thing I'm looking for, total kg CO2 emissions, I continually get an answer of 0.0. What I need is a sum of the individual total emissions as calculated in each method, i.e. the values which are printed with the following: System.out.println(trans); System.out.println(elec); and System.out.println(food);
The total should be something like 25040 or whatever, depending on the value of the inputs provided by the user, but I'm constantly getting a total of 0.0., which is obviously false. Could have something to do with the way I've initialized my variables, or something to do with the limitations of returning values from methods. I just don't know what to do. How should I tackle this? All help greatly appreciated!
import java.util.Scanner;
public class CarbonCalc {
public static void main(String[] args) {
double trans = 0;
double elec = 0;
double food = 0;
giveIntro();
determineTransportationEmission(null);
determineElecticityEmission(null);
determineFoodEmission(null);
calculateTotalEmission(trans, elec, food);
//printReport(trans, elec, food);
}
//Gives a brief introduction to the user.
public static void giveIntro() {
System.out.println("This program will estimate your carbon footprint");
System.out.println("(in metric tons per year) by asking you");
System.out.println("to input relevant household data.");
System.out.println("");
}
//Determines the user's transportation-related carbon emissions.
public static double determineTransportationEmission(Scanner input) {
Scanner console = new Scanner(System.in);
System.out.println("We will first begin with your transportation-related carbon expenditures...");
System.out.print("How many kilometres do you drive per day? ");
double kmPerDay = console.nextDouble();
System.out.print("What is your car's fuel efficiency (in km/litre)? ");
double fuelEfficiency = console.nextDouble();
System.out.println("We now know that the numeber of litres you use per year is...");
double litresUsedPerYear = 365.00 * (kmPerDay / fuelEfficiency);
System.out.println(litresUsedPerYear);
System.out.println("...and the kg of transportation-related CO2 you emit must be...");
//Final calculation of transportation-related kgCO2 emissions.
double trans = 2.3 * litresUsedPerYear;
System.out.println(trans);
System.out.println("");
return trans;
}
//Determines the user's electricity-related carbon emissions.
public static double determineElecticityEmission(Scanner input) {
Scanner console = new Scanner(System.in);
System.out.println("We will now move on to your electricity-related carbon expenditures...");
System.out.print("What is your monthly kilowatt usage (kWh/mo)? ");
double kWhPerMonth = console.nextDouble();
System.out.print("How many people live in your home? ");
double numPeopleInHome = console.nextDouble();
System.out.println("The kg of electricity-related CO2 you emit must be...");
//Final calculation of electricity-related kgCO2 emissions.
double elec = (kWhPerMonth * 12 * 0.257) / numPeopleInHome;
System.out.println(elec);
System.out.println("");
return elec;
}
//Determines the user's food-related carbon emissions.
public static double determineFoodEmission(Scanner input) {
Scanner console = new Scanner(System.in);
System.out.println("We will now move on to your food-related carbon expenditures...");
System.out.print("In a given year, what percentage of your diet is meat? ");
double meat = console.nextDouble();
System.out.print("In a given year, what percentage of your diet is dairy? ");
double dairy = console.nextDouble();
System.out.print("In a given year, what percentage of your diet is fruits and veggies? ");
double fruitVeg = console.nextDouble();
System.out.print("In a given year, what percentage of your diet is carbohydrates? ");
double carbs = console.nextDouble();
//Final calculation of food-related kgCO2 emissions.
System.out.println("The kg of food-related CO2 you emit must be...");
double food = (meat * 53.1 + dairy * 13.8 + fruitVeg * 7.6 + carbs * 3.1);
System.out.println(food);
System.out.println("");
return food;
}
//Calculates total emissions across all sources.
public static double calculateTotalEmission(double trans, double elec, double food) {
System.out.println("Your total kg of CO2 emitted across all sources is equal to...");
double total = trans + elec + food;
System.out.println((double) total);
System.out.println("");
return total;
}
}
Ah!! Thank you very much Lyju. I did the following and it all worked well.
From this:
double trans = 0;
double elec = 0;
double food = 0;
To this:
double trans = determineTransportationEmission(null);
double elec = determineElecticityEmission(null);
double food = determineFoodEmission(null);
The second problem which popped up here had to do with not correctly passing the Scanner parameter to the multiple methods.
I fixed that by adding the following to the main method:
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
double trans = determineTransportationEmission(console);
double elec = determineElecticityEmission(console);
double food = determineFoodEmission(console);
giveIntro();
calculateTotalEmission(trans, elec, food);
}
And because I had three scanner objects, one for each method, I simply removed the Scanners in each and can now pass a single Scanner from my main method to each of the others.

Categories

Resources