Passing method information - java

Can anyone help me with this problem?
import java.util.*;
public class PaintCalculator
{
public static void main(String[] args)
{
double length;
double width;
double height;
Scanner input = new Scanner(System.in);
System.out.print("Enter the length in feet: ");
length = input.nextDouble();
System.out.print("Enter the width in feet: ");
width = input.nextDouble();
System.out.print("Enter the height in feet: ");
height = input.nextDouble();
System.out.println();
PtinSqFt(length,width,height);
System.out.println("The Cost of a " + length + "- by " + width + "-foot room with a " + height + "-foot ceilings is " + newAmount + "$");
}
public static double PtinSqFt(double v1, double v2, double v3)
{
double GoP = v1*v2*v3;
double newAmount;
newAmount = GallonsOfPaint(GoP)*32;
System.out.println("The wall area for the room is " + GoP + " Square Feet!");
System.out.println("You will need " + GallonsOfPaint(GoP) + " gallons of paint!");
System.out.println("The Cost of a " + v1 + "- by " + v2 + "-foot room with a " + v3 + "- foot ceilings is " + newAmount + "$");
return newAmount;
}
public static double GallonsOfPaint(double GoP)
{
final double SQFT_OF_RM = 350;
double newgallons = (GoP/SQFT_OF_RM);
return newgallons;
}
}
It won't compile when I try to pull the return info from my PtinSqFt method?

You need to store return value of PtinSqFt(length,width,height); in a variable if you want to use it later in your println statement.
So change this
PtinSqFt(length,width,height);
to
double newAmount = PtinSqFt(length,width,height);
and it should work.

Related

Code compiles but does not run completely. When the DoWhile loop is exited red error notes pop up

import java.util.Scanner;
public class TipCalculator{
static double Steak;
static double Ribs;
static double Salad;
static double Burger;
static double SoftDrink;
static double PintofBeer;
static double Wine;
static double Champagne;
static int i;
static double [] Choice = new double [15];
static double Subtotal;
static double Tax;
static double Tip;
static double Total;
public static void main (String args []){
Scanner scan = new Scanner (System.in);
Ribs = 25;
Steak = 35;
Salad = 5;
Burger = 15;
SoftDrink = 2;
PintofBeer = 5;
Wine = 6;
Champagne = 9;
Tax = 1.13;
do {
System.out.println("Please enter one of the following options, or enter 9 to go to your bill");
System.out.println("1. 12 oz Striploin Steak ");
System.out.println("2. 16 oz Baby Back Ribs ");
System.out.println("3. Ceaser Salad ");
System.out.println("4. House Burger ");
System.out.println("5. Soft Drink ");
System.out.println("6. Wine ");
System.out.println("7. Champagne ");
Choice [i] = scan.nextInt();
}
while (Choice [i] < 9);
if (Choice [i] == 9){
Subtotal = Choice[1] + Choice[2] + Choice[3] + Choice[4] + Choice[5] + Choice[6] + Choice[7] + Choice[8] + Choice[9] + Choice[10] + Choice[11] + Choice[12] + Choice[13] + Choice[14] + Choice[15];
System.out.println("Your subtotal is " + Subtotal);
}
System.out.println("Enter your tip percent");
Tip = scan.nextInt();
Total = ((Subtotal * Tax) * (Tip/10));
System.out.println("Your total is " + Total);
}
}
You are trying to access array index 15 (so the 16th element in array) which is technically not there as index start from 0. So the last index you can access is 14. Total elements are 15 as initialized by your code.
Change your line :
Subtotal = Choice[1] + Choice[2] + Choice[3] + Choice[4] + Choice[5] + Choice[6] + Choice[7] + Choice[8] + Choice[9] + Choice[10] + Choice[11] + Choice[12] + Choice[13] + Choice[14] + Choice[15];
To:
Subtotal = Choice[0] + Choice[1] + Choice[2] + Choice[3] + Choice[4] + Choice[5] + Choice[6] + Choice[7] + Choice[8] + Choice[9] + Choice[10] + Choice[11] + Choice[12] + Choice[13] + Choice[14];
This will get the flow rolling. (Although I will confess, the code is not doing what it is meant to be doing by just reading your code. But that is a different matter.)

Error when printing integer within string

I am new to Java and was following a book with the following code...
class Vehicle {
int passengers;
int fuelcap;
int mpg;
int range() {
return mpg * fuelcap;
}
double fuelneeded(int miles) {
return (double) miles / mpg;
}
}
class TwoVehicles {
public static void main(String args[]) {
Vehicle minivan = new Vehicle();
Vehicle sportscar = new Vehicle();
double gallons;
int dist = 252;
minivan.passengers = 7;
minivan.fuelcap = 16;
minivan.mpg = 21;
sportscar.passengers = 2;
sportscar.fuelcap = 14;
sportscar.mpg = 12;
gallons = minivan.fuelneeded(dist);
System.out.println("To go ", + dist + " miles minivan needs " + gallons + " gallons of fuel.");
gallons = sportscar.fuelneeded(dist);
System.out.println("To go ", + dist + " miles sportscar needs " + gallons + " gallons of fuel.");
}
}
However, upon running this code I get an error saying 'error: no suitable method found for println(String,String)'. Why is this happening?
You can not use System.out.println with 2 arguments, pass it only 1 String.
The comma that is not inside the String (After the String "To go" ,), is telling the compiler to treat the Strings as 2 different arguments.
Change this line:
System.out.println("To go ", + dist + " miles minivan needs " + gallons + " gallons of fuel.");
to this:
System.out.println("To go " + dist + " miles minivan needs " + gallons + " gallons of fuel.");
Due to the presence of comma(,) outside the string your code is showing error
System.out.println("To go ", + dist + " miles minivan needs " + gallons + " gallons of fuel.");
Your code should be
System.out.println("To go ," + dist + " miles minivan needs " + gallons + " gallons of fuel.");

Running a Java file from Eclipse on Mac

I'm writing a simple Java program for my history final project and am having trouble exporting it out of eclipse so it can be run on my teacher's computer (a Mac).
Here is the code:
package project;
import java.util.Scanner;
public class Denim {
public static void main(String []args) {
Scanner scan = new Scanner(System.in);
System.out.println("What item of denim is in question?");
String str = scan.nextLine();
String str1 = str.toUpperCase();
String jeans = "JEANS";
String jeansp = "JEAN PANTS";
String tux = "CANADIAN TUXEDO";
String tux1 = "CANADIEN TUXEDO";
String tux2 = "CANADIAN TUX";
String tux3 = "CANADIEN TUX";
String jacket = "JACKET";
String jjacket = "JEAN JACKET";
String hat = "HAT";
String dhat = "DENIM BASEBALL HAT";
String bhat = "BASEBALL HAT";
String c = "C";
int jeanFactor = 9982;
double jacketFactor = 10466.25178;
double bottleFactor = 128/16.9;
double hatFactor = 314.1415;
if (str1.equals(jeans) || str1.equals(jeansp)){
System.out.println("How many pairs of jeans?");
int numj = scan.nextInt();
int gallonj = numj*jeanFactor;
System.out.println("Producing " + numj + " pairs of jeans would use: ");
System.out.println(gallonj + " gallons of water");
double bottlesj = gallonj*bottleFactor;
System.out.println(bottlesj + " bottles of water");}
else if ((str1.equals(tux)) || (str1.equals(tux1)) || (str1.equals(tux2)) ||(str.equals(tux3))){
System.out.println("How many tuxedos?");
int numt = scan.nextInt();
int gallontp = numt * jeanFactor;
double gallontj = numt * jacketFactor;
double gallont = gallontp + gallontj;
double bottlest = gallont*bottleFactor;
System.out.println("Producing " + numt +" " + c + str.substring(1,8) + " tuexedos would use: ");
System.out.println(gallont +" gallons of water.");
System.out.println(bottlest + " bottles of water");}
else if (str1.equals(jacket) || str.equals(jjacket)){
System.out.println("How many jackets?");
int numjj = scan.nextInt();
double gallonjj = numjj*jacketFactor;
double bottlesjj = numjj*bottleFactor;
System.out.println("Producing " + numjj + " jackets would use: ");
System.out.println(gallonjj + " gallons of water");
System.out.println(bottlesjj + " bottles of water");}
else if (str1.equals(hat) || str.equals(dhat) || str.equals(bhat)){
System.out.println("How many hats?");
int numh = scan.nextInt();
double gallonh = numh*hatFactor;
double bottlesh = numh*bottleFactor;
System.out.println("Producing " + numh + " jackets would use: ");
System.out.println(gallonh + " gallons of water");
System.out.println(bottlesh + " bottles of water");
}
}
}
I click file-export and export it as a .jar file, but every time I try and open it to run it, a message pops up saying "The Java JAR file could not be launched. Does anyone know how to fix this or what I'm doing wrong? Both my computer and my teacher's are Macbooks.

No main methods, applets, or MIDlets found

When I run this a popup comes up telling me no main methods, applets, or MIDlets ound. Can someone tell me why please?--I am using jGrasp. I am trying to use main method and I haven't had this issue before.
import java.util.*;
import java.io.*;
public class ProgrammingExercise3.1
{
public static void main(String[] args);
{
double rectWidth;
double rectLength;
double radius;
int age;
double begBal;
char A;
String name;
double rate;
scanner inFile = new Scanner(new FileReader("C:\\Users\\sierr_000\\Desktop\\Sam School\\IT-145\\Exercises\\Ch 3\\inData.txt"));
PrintWriter outFile = new PrintWriter("C:\\Users\\sierr_000\\Desktop\\Sam School\\IT-145\\Exercises\\Ch 3\\outData.out");
rectWidth = inFile.next();
rectLength = inFile.next();
outFile.println("Rectangle: ")
outFile.println("Length = " + rectLength + ", width = " + rectWidth + ", area = "
+ (rectWidth*rectLength) + ", perimeter = " + (2 * (rectwidth + rectLengh)));
radius = inFile.next();
outFile.println("Circle: ");
outfile.println("Radius = " + radius + ", area = " + (radius*3.1416) + "Circumfrence = " + (2*3.1416*radius));
name = inFile.next();
age = inFile.next();
begBal = inFile.next();
rate = inFile.next();
outFile.println("Name: " + name + ", age: " + age);
outFile.printf("Beginning Balance: %7.2f" , begBal + "interest rate: %4.2f" , rate);
outFile.println("The character that comes after A in the ASCII is B");
inFile.close();
outFile.close();
}
}
This seems to be full of errors, but the main reason you are getting that error is that you put a semicolon after public static void main(String[] args).

Was told the code I have is not Recursion...Looking for Guidance to why not!

It was pointed out to me that the statement below is not recursion. I thought recursion only means that it calls itself until the answer is found. What would make this recursion?
public static double totalDistance(int[] x, int[] y, String[] city, int i){
double xSub = x[i] - x[i-1];
double ySub = y[i] - y[i-1];
double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
distance = Math.round(Math.sqrt(distance));
System.out.println("Distance From " + city[i] + " to " + city[i-1] + " is " + distance + " miles.");
if (i == 1){
return distance;
}
else {
return distance+totalDistance(x,y,city, i-1);
}
}
This is entire code below in case anyone is curious to what is going on...
import java.util.Scanner;
class distance {
public static void main(String[] args) {
System.out.println("Welcome to Travel Bliss Distance Calculator!");
Scanner input = new Scanner(System.in);
int[] x = new int[5];
int[] y = new int[5];
String[] city = new String[5];
int i=0;
for (i=0; i < 5;i++){
System.out.println("Enter City>>");
city[i] = input.next();
System.out.println("Enter X Coordinates>>");
x[i] = input.nextInt();
System.out.println("Enter Y Coordinates>>");
y[i] = input.nextInt();
System.out.println("You Entered: " + city[i] + " with Coordinates: (" + x[i] + "," + y[i] + ") ");
}
i = i-1;
System.out.println("============================================================");
System.out.println("Calculating Distance Between: " + city[0] +", " + city[1] + ", " + city[2] + ", " + city[3] + ", " + city[4]+" >>>");
System.out.println("TOTAL of: "+ totalDistance(x, y, city, i)+ " miles.");
}
public static double totalDistance(int[] x, int[] y, String[] city, int i){
double xSub = x[i] - x[i-1];
double ySub = y[i] - y[i-1];
double distance = Math.pow(xSub, 2) + Math.pow(ySub, 2);
distance = Math.round(Math.sqrt(distance));
System.out.println("Distance From " + city[i] + " to " + city[i-1] + " is " + distance + " miles.");
if (i == 1){
return distance;
}
else {
return distance+totalDistance(x,y,city, i-1);
}
}
}
The totalDistance(...) function is indeed recursive (since it calls itself).
It is recursion -- what they might have meant (or you misunderstood) is that it's not "tail recursion".
This is a subset of recursion that is very simple to optimize into a simple loop (although, Java does not do that yet). To be tail-recursive, you have to return the result of the recursive call -- in your case, you add to it first.
looks like recursion to me. who told you it wasn't?
It IS recursive. Probably the critic is it follows too closely the iterative (loop) version.
Probably then were expecting that, instead of going through the list from beginning to end, you picked the middle city, calculated the distance from the start to that city and from that city to the end and added that. Each half of the distance would be calculated calling the same function.

Categories