I keep getting an error where Java, instead of creating a .class file for the file, it gives me the error on command prompt
public class TotalAndAverage {
public static void main (String[] arg) {
final int count = 6;
double score0 = 98.05;
double score1 = 100.00;
double score2 = 80.50;
double score3 = 62.65;
double score4 = 77.25;
double score5 = 90.93;
double total = score0 + score1 + score2 + score3 + score4 + score5;
double average = total/count;
int averageInt = (int)average;
int average2Digit = (int)(average * 100) - averageInt *100;
System.out.println("Total Score is: \t" + total);
System.out.println("Average is \t" + average);
system.out.println("Average Score is: \t" + averageInt + "." + average2Digit);
}
}
It shows
error: package system does not exist
Last line should be
System.out.println(.....)
not system.
When you say something.somethingElse the compiler assumes you are doing a packageName.classname
BTW, system.out.println("Average Score is: \t" + averageInt + "." + average2Digit); make the system part as System.
Related
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.)
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.");
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).
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.
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.