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.");
Related
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.
All of my main methods take place in this class:
package wk2individual;
import java.util.Scanner;
public class Wk2Individual {
public static void main(String[] args) {
AnnualPayCalculator aPC = new AnnualPayCalculator();
SalesPerson sP = new SalesPerson();
//System greeting
Scanner sc = new Scanner (System.in);
System.out.println ("Welcome to the Employee Annual Pay calculator!");
//user input
System.out.println("Please enter the name of the first sales employee:");
sP.salesPerson1 = sc.next();
System.out.println ("Please enter " + sP.salesPerson1 + "'s total sales for the year:");
aPC.totalSales1 = sc.nextDouble();
//begin outputs
if (aPC.totalSales1 >= 112000 && aPC.totalSales1 < 140000) {
System.out.println(sP.salesPerson1 + " has earned $" + aPC.total1() + " in "
+ "commissions for the year! " + sP.salesPerson1 + "'s total pay for the "
+ "year will be $" + aPC.total2()); //outputs employees commission and pay if sales meet incentive
}
else if (aPC.totalSales1 >= 140000) {
System.out.println(sP.salesPerson1 + " has earned $" + aPC.total3() + " in "
+ "commissions for the year! " + sP.salesPerson1 + "'s total pay for the "
+ "year will be $" + aPC.total4()); //outputs employees commission and pay if sales exceed targetSales
}
else if (aPC.totalSales1 < 112000) {
System.out.println(sP.salesPerson1 + " will receive a total pay of $" +
aPC.fixedSalary + " for the year. " + sP.salesPerson1 + " did not meet "
+ "the sales incentive to earn commission for the year."); /*outputs employees end of year pay as fixed
salary since the sales amount is less than 80% of the sales target*/
}
//begin the inputs for the second salesperson
System.out.println("Now let's get the name of the second sales employee:");
sP.salesPerson2 = sc.next();
System.out.println("Please enter " + sP.salesPerson2 + "'s total sales for the year:");
aPC.totalSales2 = sc.nextDouble();
//begin outputs
if (aPC.totalSales2 >= 112000 && aPC.totalSales2 < 140000) {
System.out.println(sP.salesPerson2 + " has earned $" + aPC.total5() + " in "
+ "commissions for the year! " + sP.salesPerson2 + "'s total pay for the "
+ "year will be $" + aPC.total6()); //outputs employees commission and pay if sales meet incentive
}
else if (aPC.totalSales2 >= 140000) {
System.out.println(sP.salesPerson2 + " has earned $" + aPC.total7() + " in "
+ "commissions for the year! " + sP.salesPerson2 + "'s total pay for the "
+ "year will be $" + aPC.total8()); //outputs employees commission and pay if sales exceed targetSales
}
else if (aPC.totalSales2 < 112000) {
System.out.println(sP.salesPerson2 + " will receive a total pay of $" +
aPC.fixedSalary + " for the year. " + sP.salesPerson2 + " did not meet "
+ "the sales incentive to earn commission for the year."); /*outputs employees end of year pay as fixed
salary since the sales amount is less than 80% of the sales target*/
}
//This is where I am trying to print the array created in the SalesPerson class
System.out.println("");
System.out.println("Here are both employee's sales in comparison:");
System.out.println(sP.salesPerson1 + "\t" + sP.salesPerson2);
System.out.print(n);
}
}
I created the AnnualPayCalculator class to hold the totals and calculations:
package wk2individual;
public class AnnualPayCalculator
{
double totalSales1, totalSales2, employee1TotalPay, employee2TotalPay;
double fixedSalary = 75000.00;
final double commissionRate = .25;
double salesTarget = 140000;
double accelerationFactor = .3125;
double total1(){
double incentiveCommission = totalSales1 * commissionRate;
return incentiveCommission;
}
double total2(){
double employee1TotalPay = total1() + fixedSalary;
return employee1TotalPay;
}
double total3(){
double targetCommission = totalSales1 * accelerationFactor;
return targetCommission;
}
double total4(){
double employee1TotalPay = total3() + fixedSalary;
return employee1TotalPay;
}
double total5(){
double incentiveCommission = totalSales2 * commissionRate;
return incentiveCommission;
}
double total6(){
double employee2TotalPay = total5() + fixedSalary;
return employee2TotalPay;
}
double total7(){
double targetCommission = totalSales2 * accelerationFactor;
return targetCommission;
}
double total8(){
double employee2TotalPay = total7() + fixedSalary;
return employee2TotalPay;
}
}
Then I created this SalesPerson class in which holds my array:
package wk2individual;
public class SalesPerson {
String salesPerson1, salesPerson2;
public static void main(String[] args) {
AnnualPayCalculator aPC = new AnnualPayCalculator();
Double[][] sales = new Double[2][2];
sales[0][0] = aPC.totalSales1;
sales[0][1] = aPC.totalSales2;
sales[1][0] = aPC.employee1TotalPay;
sales[1][1] = aPC.employee2TotalPay;
printArray(sales);
};
private static void printArray(Double[][] numbers){
for (Double[] n : numbers){
System.out.print(n);
}
}
In the first class I am able to print the totals of the calculations defined in the AnnualPayCalculator class. How can I print the array in the first class?
You probably don't want 2 main methods. When you create an object of SalesPerson in Wk2Individual, the 2d array sales is not being declared because static methods and variables are not part of instances/objects of classes. So what you might want to do is make a non-static method in SalesPerson like this;
public class SalesPerson {
String salesPerson1, salesPerson2;
public void createSales(AnnualPayCalculator aPC) {
// you don't need to create aPC
// AnnualPayCalculator aPC = new AnnualPayCalculator();
Double[][] sales = new Double[2][2];
sales[0][0] = aPC.totalSales1;
sales[0][1] = aPC.totalSales2;
sales[1][0] = aPC.employee1TotalPay;
sales[1][1] = aPC.employee2TotalPay;
printArray(sales);
}
}
Also, you are probably trying to use the values from the aPC object in the Wk2Individual class. But you are creating a new instance of the object instead. So you should pass the old aPC object from Wk2Individual class like this:
System.out.println("");
System.out.println("Here are both employee's sales in comparison:");
System.out.println(sP.salesPerson1 + "\t" + sP.salesPerson2);
sP.createSales(aPC);
This will send the aPC object with all the calculated values to the createSales() of SalesPerson class where your 2d array will be created.
Now you need to print this. To do that create a print method in the SalesPerson class:
private void printArray(Double[][] numbers){
for (Double[] n : numbers){
System.out.print(n);
}
}
But you cannot print an array like that. So do this:
System.out.println(Arrays.toString(n));
In AnnualPayCalculator class you have several methods which use the global variables: employee1TotalPay and employee2TotalPay. For example, the method total2(). In these methods, you are creating yet another variable with the same name. In total2() you are creating employee1TotalPay which shadows the global variable employee1TotalPay. It means that if inside that method you use employee1TotalPay anywhere, it will use the local employee1TotalPay variable (the one you created inside the method). To use the global variable either remove the declaration of the local variable:
employee1TotalPay = total1() + fixedSalary;
or use the this keyword to access the global variables:
this.employee1TotalPay = total1() + fixedSalary;
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.
I'm trying to write this compounding interest program with a do while loop at the end and I cannot figure out how to print out the final amount.
Here is the code I have so far :
public static void main(String[] args) {
double amount;
double rate;
double year;
System.out.println("This program, with user input, computes interest.\n" +
"It allows for multiple computations.\n" +
"User will input initial cost, interest rate and number of years.");
Scanner keyboard = new Scanner(System.in);
System.out.println("What is the initial cost?");
amount = keyboard.nextDouble();
System.out.println("What is the interest rate?");
rate = keyboard.nextDouble();
rate = rate/100;
System.out.println("How many years?");
year = keyboard.nextInt();
for (int x = 1; x < year; x++){
amount = amount * Math.pow(1.0 + rate, year);
}
System.out.println("For " + year + " years an initial " + amount + " cost compounded at a rate of " + rate + " will grow to " + amount);
String go = "n";
do{
System.out.println("Continue Y/N");
go = keyboard.nextLine();
}while (go.equals("Y") || go.equals("y"));
}
}
The trouble is, amount = amount * Math.pow(1.0 + rate, year);. You're overwriting the original amount with the calculated amount. You need a separate value to hold the calculated value while still holding the original value.
So:
double finalAmount = amount * Math.pow(1.0 + rate, year);
Then in your output:
System.out.println("For " + year + " years an initial " + amount +
" cost compounded at a rate of " + rate + " will grow to " + finalAmount);
EDIT: Alternatively, you can save a line, a variable, and just do the calculation inline, as such:
System.out.println("For " + year + " years an initial " + amount +
" cost compounded at a rate of " + rate + " will grow to " +
(amount * Math.pow(1.0 + rate, year)));