my dog trithlon program gives errors - java

public class DogTriathlonParticipant {
/**
* #param args the command line arguments
*/
static {
}
public static void main(String[] args) {
{
{
private static int NUM_EVENTS;
private static int totalCumulativeScore = 0;
private final string name;
private final int obedienceScore;
private final int conformationScore;
private final int agilityScore;
private final int total;
private final double avg;
private String name;
public DogTriathlonParticipant (String name, int numEvents, int score1,
int score2, int score3)
{
{
{
{
this.name = name;
NUM_EVENTS = numEvents;
obedienceScore = score1;
conformationScore = score2;
agilityScore = score3;
total = obedienceScore + conformationScore + agilityScore;
avg = (double) total / NUM_EVENTS;
totalCumulativeScore = totalCumulativeScore + total;
{
{
{
}
}
}
{
public void display()
{
System.out.println(name + " participated in " + NUM_EVENTS + " Events and has an average score of " + avg);
System.out.println( " " + name + " has total score of " + total + " bringing the total cumlative score to " + totalCumulativeScore);
}
public class TestDogs
{
}
public static void main(String[] args)
{
{
{
DogTriathlonParticipant dog1 =
new DogTriathlonParticipant("Bowser" , 2, 85, 89,0);
dog1.display();
{
}
DogTriathlonParticipant dog2 =
new DogTriathlonParticipant("Rush", 3, 78, 72, 80);
dog2.display();
{
}
DogTriathlonParticipant dog3 =
new DogTriathlonParticipant("Ginger", 3, 90, 86, 72);
dog3.display();
}
}
}
}
it keeps giving errors to my names and string and also the public voids
maybe I just need a fresh eye to help me figure out the errors and why they say they are illegal
and fi you could figure it out can you also explain to me why so I can learn why?
I'm using netbeans and rather new to java

If you goal is to create DogTriathlonParticipant instances and "test" the instanciation by displaying the information, you may first separate your logic in different java classes / pages.
You should create new Java class in netbeans for the main, and the DogTriathlonParticipant class. To do so, click on Source Packages > new > Java Class.
Name that class DogTriathlonParticipant as show below :
public class DogTriathlonParticipant {
//private static int NUM_EVENTS;
//private static int totalCumulativeScore = 0; //this logic is not part of DogTriathlonParticipant.
// you should not put final if you plan to edit the variable.
private String name;
private int obedienceScore;
private int conformationScore;
private int agilityScore;
private int total;
private double avg;
private String name;
public DogTriathlonParticipant (String name, int numEvents, int score1, int score2, int score3) {
this.name = name;
// NUM_EVENTS = numEvents; this logic don't belong here
obedienceScore = score1;
conformationScore = score2;
agilityScore = score3;
total = obedienceScore + conformationScore + agilityScore;
avg = (double) total / numEvents; // edited here, I think you are trying to get the average of to total by numEvents, in this case, the score1 shoulb be the sum of the events "numEvents".
//totalCumulativeScore = totalCumulativeScore + total;
}
public void display()
{
System.out.println(name + " participated in " + numEvents+ " Events and has an average score of " + avg);
System.out.println( " " + name + " has total score of " + total + " bringing the total cumlative score to " + total);
}
}
Then you should have your "test" class like this :
public class TestDogs {
public static void main(String[] args) {
DogTriathlonParticipant dog1 = new DogTriathlonParticipant("Bowser" , 2, 85, 89,0);
DogTriathlonParticipant dog2 = new DogTriathlonParticipant("Rush", 3, 78, 72, 80);
DogTriathlonParticipant dog3 = new DogTriathlonParticipant("Ginger", 3, 90, 86, 72);
dog1.display();
dog2.display();
dog3.display();
}
}
With that, you should have a good base to start and add your logic and correct the one in place.

Related

Can't get a basic method to work

Can anyone please tell why the getSpeed method does not work ?
Whenever I hover over the method I get the :
to insert ;
illegal modifier
syntax error please insert []
syntax error on token
public class Tanks {
private String TankName;
private int TankModel;
private int CrewNumber;
private double Speed;
private int TurretSpeed;
Tanks (String name, int model, int crew, double speed, int turretspeed){
this.TankName = name;
this.TankModel = model;
this.CrewNumber = crew;
this.Speed = speed;
this.TurretSpeed = turretspeed;
}
public static void main(String[] args) {
Tanks merkava = new Tanks ("MERKAVA", 1, 5, 56.64, 67);
Tanks judge = new Tanks ("JUDGE", 2, 6, 66.66, 68);
public double getSpeed() {
return Speed;
}
System.out.println(merkava.TankName+ " "+merkava.TankModel+" "+merkava.CrewNumber+" "+merkava.Speed+" "+merkava.TurretSpeed);
System.out.println(judge.TankName+ " "+judge.TankModel+" "+judge.CrewNumber+" "+judge.Speed+" "+judge.TurretSpeed);
}
}
Your main method contains the following method.
public double getSpeed() {
return Speed;
}
Please move this to Tanks class.
Following should work:
public class Tanks {
private String TankName;
private int TankModel;
private int CrewNumber;
private double Speed;
private int TurretSpeed;
Tanks(String name, int model, int crew, double speed, int turretspeed) {
this.TankName = name;
this.TankModel = model;
this.CrewNumber = crew;
this.Speed = speed;
this.TurretSpeed = turretspeed;
}
public double getSpeed() {
return Speed;
}
public static void main(String[] args) {
Tanks merkava = new Tanks("MERKAVA", 1, 5, 56.64, 67);
Tanks judge = new Tanks("JUDGE", 2, 6, 66.66, 68);
System.out.println(merkava.TankName + " " + merkava.TankModel + " " + merkava.CrewNumber + " " + merkava.Speed
+ " " + merkava.TurretSpeed);
System.out.println(judge.TankName + " " + judge.TankModel + " " + judge.CrewNumber + " " + judge.Speed + " "
+ judge.TurretSpeed);
}
}
If you have gone through some basic Java course you will know that by defining a method (in this case getSpeed()) inside another function (main) is wrong.

Assigning Random Number to Array of Objects

I am trying to assign a random number to an array of objects and I am trying to assign it to a variable called score. My main class code is below:
import java.util.Random;
public class App {
public static void main(String[] args) {
Bands[] bands = new Bands[5];
bands[0] = new Bands("Joe", "Rick", "Nick", "Dalton", "Doylestown, PA", "RockOn", 4000.50 , "Rock");
bands[1] = new Bands("Luke", "Bill", "Ian", "Matt", "State College, PA", "Blink182", 3500.50 , "Alternative");
bands[2] = new Bands("Corey", "Noah", "Jon", "Kaleb", "Philadelphia, PA", "Rise Against", 10000.50 , "Rock");
bands[3] = new Bands("Jake", "Joey", "Mike", "Mac", "New York, NY", "Thousand Foot Krutch", 2000.50 , "Rock");
bands[4] = new Bands("Bob", "Jeff", "Dom", "Mark", "King of Prussia, PA", "Skillet", 5500.50 , "Rock");
int score = compete(score);
}
}
And then my Bands class code is located below. I am having trouble getting it to work properly:
import java.util.Random;
public class Bands {
private String singerName;
private String guitarName;
private String bassistName;
private String drummerName;
private String Hometown;
private String bandName;
private double income;
private String genre;
private int score;
public Bands(String singerName, String guitarName, String bassistName, String drummerName, String Hometown, String bandName, double income, String genre)
{
this.singerName = singerName;
this.guitarName = guitarName;
this.bassistName = bassistName;
this.drummerName = drummerName;
this.bandName = bandName;
this.Hometown = Hometown;
this.income = income;
this.genre = genre;
this.score = -1;
}
public int compete()
{
Random rand = new Random();
this.score = rand.nextInt(20);
return score;
}
public String getInfo()
{
String bandInfo = "Band: " + this.bandName + ", Singer: " + this.singerName + ", Guitarist: " + this.guitarName + ", Bassist: " + this.bassistName +
", Drummer: " + this.drummerName + ", Hometown: " + this.Hometown + ", Income: " + this.income + ", Genre: " +
this.genre + ", Final Score: " + this.score;
return bandInfo;
}
Your class Bands have method compete that not take any parameter and in your main method you call a method compete that take a score :
int score = compete(score);
So i assume that you want to call your compete method that exist in another class so to solve this issue yo should :
Create setter of your attribute score in Bands class
Create a new Bands object :
Set the Score
Call your compete method
Bands b = new Bands();
b.setScore(score);
int score = b.compete();
Or
change the compete method to take an attribute score
public int compete(int score){
Random rand = new Random();
this.score = rand.nextInt(20);
return score;
}

java - Function in class

I need to write a function to College department :
Add function adds additional lecturer.
Action returns false if there is no place to add additional lecturer, and at the same true if the lecturer was successfully added.
What I had written so far:
public boolean newLecturer(Lecturer[] AllLecturer) {
int MaxLecturer = 0;
MaxLecturer = this.maxLecturer;
int sum = 0;
sum += 1;
if (sum < MaxLecturer) {
System.out.println("true");
return true;
}
else {
System.out.println("false");
return false;
}
}
The function does not work properly, It always returns true (because that the Max Lecturer always bigger than sum).
main:
public class main {
public static void main(String[]args){
Lecturer[] L1 = new Lecturer[]{new Lecturer("David",3,"Banana",1001)};
Lecturer[] L2 = new Lecturer[]{new Lecturer("Yossi",5,"apple",1002)};
Lecturer[] L3 = new Lecturer[]{new Lecturer("Y",2,"t",1003)};
College myCollege = new College("College1",20,L1,3);
//System.out.println(myCollege);
//myCollege.allLecturer=L2;
//System.out.println(myCollege);
myCollege.newLecturer(L1);
myCollege.newLecturer(L2);
myCollege.newLecturer(L3);
}
}
class College (Function here):
public class College {
public String name;
public int numOfLecturer;
public Lecturer[] allLecturer;
public int maxLecturer;
// constructor
public College(String Name, int NumOfLecturer, Lecturer[] AllLecturer,
int MaxLecturer) {
this.name = Name;
this.numOfLecturer = NumOfLecturer;
this.allLecturer = AllLecturer;
this.maxLecturer = MaxLecturer;
}
public College(String Name) {
this.name = Name;
}
public College(Lecturer[] AllLecturer) {
this.allLecturer = AllLecturer;
}
public boolean newLecturer(Lecturer[] AllLecturer) {
int MaxLecturer = 0;
MaxLecturer = this.maxLecturer;
int sum = 0;
sum += 1;
if (sum < MaxLecturer) {
System.out.println("true");
return true;
}
else {
System.out.println("false");
return false;
}
}
#Override
public String toString() {
String lecturers = "";
for (Lecturer lecturer : allLecturer) {
lecturers += lecturer;
}
return "[Name College: " + name + "] " + " [num Of Lecturer: "
+ numOfLecturer + "]" + " [all Lecturer: " + lecturers + "]"
+ " [max Lecturer " + maxLecturer + "]";
}
}
class Lecturer:
public class Lecturer {
public String name;
public int numOfTimesPenFalls;
public String favoriteIceCream;
public int autoNumber;
// constructor
public Lecturer(String Name, int NumOfTimesPenFalls,
String FavoriteIceCream, int AutoNumber) {
this.name = Name;
this.numOfTimesPenFalls = NumOfTimesPenFalls;
this.favoriteIceCream = FavoriteIceCream;
this.autoNumber = AutoNumber;
}
public Lecturer(String Name) {
this.name = Name;
}
#Override
public String toString() {
return "[name: " + name + "] " + " [num Of Times Pen Falls: "
+ numOfTimesPenFalls + "] " + " [favorite Ice Cream: "
+ favoriteIceCream + "] " + " [auto Number: " + autoNumber
+ "]";
}
}
And finally how can I print it?
Like this gives a compiler error:
myCollege.newLecturer("David",2,"Apple",1004);
thank you.
You're new; you need a lot of help.
Start by learning and following Java coding standards. Variable names should start with lower case. Classes start with upper. Deviations from that make your code hard to read.
Your method is wrong. You need something like this inside that class:
private static final int MAX_LECTURERS = 3;
private int numLecturers = 0;
private Lecturer [] lecturers = new Lecturer[MAX_LECTURERS];
public boolean addLecturer(Lecturer lecturer) {
boolean addedLecturer = false;
if (this.numLecturers < MAX_LECTURERS) {
this.lecturers[numLecturers++] = lecturer;
addedLecturer = true;
}
return addedLecturer;
}
Here's how you use this method:
Lecturer newLecturer = new Lecturer("foo", 1, "bar", 3);
college.addLecturer(newLecturer);
Please stop with all that array nonsense. The array is inside the College class.
The sum variable in your code is a local variable, its scope is only at the function level. This means the sum always get initialized to 0 and increased to 1 every time the function newLecturer() is called. That's why sum always smaller than MAX_LECTURER (1<3).
You need to use class variable numLecturers like in duffymo answer above.

Why dosen't my for each loop return anything?

The purpose of my code is to have the user input the name of a car. The code will use a for loop to search through an array list and print out the car that the user typed in. The code compiles, but it doesn't print anything out. Here is the code:
public class searchList
{
private static inventory inventory = new inventory();
private static ArrayList<engineSpecs> list = inventory.getList();
public void searchList()
{
//Declarations
Scanner scan = new Scanner(System.in);
String search = new String();
String car = new String();
//Prompts user to enter car name
System.out.println ("Enter car name: ");
car = scan.nextLine();
//Searches array list and prints car
for (engineSpecs item: list)
{
if (item.equals(car))
{
System.out.println (item);
}
}
}
}
The array list is declared in another class. Here is that class:
public class inventory
{
public static ArrayList<engineSpecs> list = newArrayList<engineSpecs>();
public inventory()
{
//Adds objects into array list
engineSpecs astonmartin = new engineSpecs("Aston Martin", "Vanquish", 350000, 11, "Gray",
2015, 565, 457, "automatic");
engineSpecs ferrari = new engineSpecs ("Ferrari", "458 Italia", 240000, 13, "Red", 2015,
570, 398, "automatic");
list.add(astonmartin);
list.add(ferrari);
}
public static ArrayList<engineSpecs> getList()
{
//getter method
return list;
}
}
My engineSpecs class is just a simple constructor. I'm using it so I can instantiate an object of this constructor and save it into the array list.
public class engineSpecs
{
private int HP;
private int torque;
private String transmission;
private int year;
private int MPG;
private int price;
private String model;
private String color;
private String manufacturer;
public engineSpecs (String manufacturerName, String modelName,int stickerPrice, int MPGe,
String extColor, int yearNum, int BHP, int torquelbs, String transmissionType)
{
HP = BHP;
torque = torquelbs;
transmission = transmissionType;
year = yearNum;
MPG = MPGe;
price = stickerPrice;
model = modelName;
color = extColor;
manufacturer = manufacturerName;
}
public String toString()
{
String result = "Manufacturer: " + manufacturer + "\n";
result += "Model: " + model + "\n";
result += "Price: " + price + "\n";
result += "Estimated MPG: " + MPG + "\n";
result += "Color: " + color + "\n";
result += "Year: " + year + "\n";
result += "Horse power: " + HP + "\n";
result += "Torque: " + torque + "\n";
result += "Transmission: " + transmission + "\n";
return result;
}
}
Your are using .equals() for objects which is not going to return true until they are same object in the memory. You need a new method that compares the string value of the name. Also you need toString() method to print the string conversion of the object.
Your engineSpecs class should look something like this:
public class engineSpecs{
public String name;
public engineSpecs(String s){
this.name = s;
}
public boolean is(String car){
return this.name.equals(car);
}
public String toString(){
return this.name;
}
}
Update:
change your if statement to :
if (item.is(car))
{
System.out.println (item);
}
Define this method in engineSpecs :
public boolean is(String manufacturerName){
return this.manufacturer.equals(manufacturerName);
// need to compare more values if you want to compare more properties
}
You can try to redefine the method equals() in your class engineSpecs, so the line item.equals(car) works correctly.
More info here.

Syntax errors in pizza order program

Can anybody help me with this?
simple pizza order program
I tried to run it in commandpromt and there are a lot of error
I have tried to change the double into int.. but the result is still error
<pre>
public class PizzaOrder
{
public static final String PIZZA_SMALL = "S";
public static final String PIZZA_MEDIUM = "M";
public static final String PIZZA_LARGE = "L";
public static final String PIZZA_COLLOSAL = "C";
public static final double SMALL_DIAMETER = 9;
public static final double MEIDUM_DIAMETER = 13;
public static final double LARGE_DIAMETER = 17;
public static final double COLOSSAL_DIAMETER = 26;
public static final double PRICE_SMALL = 8;
public static final double PRICE_MEDIUM = 11;
public static final double PRICE_LARGE = 15;
public static final double PRICE_COLOSSAL = 21;
public static final double PRICE_TAX = 0.095;
public static final double PRICE_TOPPING = 0.99;
public static final int MAX_TOPPINGS = 8;
public static final int MIN_TOPPINGS = 0;
/**
* Pizza Order
*
* #param args command-line arguments
*/
public static int getDiameter(String pizzaName)
{
if (pizzaName.equals(PIZZA_SMALL))
{
return SMALL_DIAMETER;
}
else if (pizzaName.equals(PIZZA_MEIDUM))
{
return MEDIUM_DIAMETER;
}`enter code here`
else if (pizzaName.equals(PIZZA_LARGE))
{
return LARGE_DIAMETER;
}
else
{
return COLOSSAL_DIAMETER;
}
}
public static int getBasePrice(String pizzaName)
{
if (pizzaName.equals(PIZZA_SMALL))
{
return PRICE_SMALL;
}
else if (pizzaName.equals(PIZZA_MEIDUM))
{
return PRICE_MEDIUM;
}
else if (pizzaName.equals(PIZZA_LARGE))
{
return PRICE_LARGE;
}
else
{
return PRICE_COLOSSAL;
}
}
there are error about the scanner too idk why
there are 13-20 errors and mostly because of the variables PIZZA_SMALL, etc
some errors say "incompetible types" and the other says "cannot find symbol"
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter The Size of Pizza you"
+ "want: (S/M/L/C)");
String option = keyboard.nextLine().trim().substring(0,
1).toUppercase();
double pizzaPrice;
double pizzaSize;
if(option.equals(PIZZA_SMALL))
{
pizzaPrice = SMALL_DIAMETER;
pizzaSize = SMALL_DIAMETER;
}
else if (option.equals(PIZZA_MEIDUM))
{
pizzaPrice = PRICE_MEDIUM;
pizzaSize = MEDIUM_DIAMETER;
}
else if (option.equals(PIZZA_LARGE))
{
pizzaPrice = PRICE_LARGE;
pizzaSize = LARGE_DIAMETER;
}
else
{
option = PIZZA_COLOSSAL;
pizzaPrice = PRICE_COLOSSAL;
pizzaSize = COLOSSAL_DIAMETER;
}
System.out.println("Pizza Size: " + option);
System.out.println("Enter The Number of Toppings" +
"you want:(0-8)");
int pizzaTopping = keyboard.nextInt();
if(pizzaTopping < MIN_TOPPINGS)
{
pizzaTopping = MIN_TOPPINGS;
}
else if(pizzaTopping > MAX_TOPPINGS)
{
pizzaTopping = MAX_TOPPINGS;
}
else
{
pizzaTopping = pizzaTopping;
}
int radius = getDiameter(option) / 2;
double squareInches = radius * radius * Math.PI;
System.out.println("Pizza Size: " + option + "( " + pizzaSize +
"inch -- " + squareInches + " square inches)" );
System.out.println("Toppings: " + pizzaTopping);
double priceWithToppings = getBasePrice(option) + pizzaTopping * 9;
System.out.println("Price: " + priceWithToppings);
double pizzaTax = priceWithToppings * PRICE_TAX;
System.out.println("Tax: "+ pizzaTax);
double totalPrice = priceWithToppings + pizzaTax;
System.out.println("Total Price: " + totalPrice);
double priceEachSquareInch = priceWithToppings / squareInches;
System.out.println("Price/sq.in.: " + priceEachSquareInch);
}
}
Your PizzaOrder class should be as follows:
public class PizzaOrder {
public static final String PIZZA_SMALL = "S";
public static final String PIZZA_MEDIUM = "M";
public static final String PIZZA_LARGE = "L";
public static final String PIZZA_COLLOSAL = "C";
public static final double SMALL_DIAMETER = 9;
public static final double MEDIUM_DIAMETER = 13;
public static final double LARGE_DIAMETER = 17;
public static final double COLOSSAL_DIAMETER = 26;
public static final double PRICE_SMALL = 8;
public static final double PRICE_MEDIUM = 11;
public static final double PRICE_LARGE = 15;
public static final double PRICE_COLOSSAL = 21;
public static final double PRICE_TAX = 0.095;
public static final double PRICE_TOPPING = 0.99;
public static final int MAX_TOPPINGS = 8;
public static final int MIN_TOPPINGS = 0;
/**
* Pizza Order
*
* #param args
* command-line arguments
*/
public static double getDiameter(String pizzaName) {
if (pizzaName.equals(PIZZA_SMALL)) {
return SMALL_DIAMETER;
} else if (pizzaName.equals(PIZZA_MEDIUM)) {
return MEDIUM_DIAMETER;
} else if (pizzaName.equals(PIZZA_LARGE)) {
return LARGE_DIAMETER;
} else {
return COLOSSAL_DIAMETER;
}
}
public static double getBasePrice(String pizzaName) {
if (pizzaName.equals(PIZZA_SMALL)) {
return PRICE_SMALL;
} else if (pizzaName.equals(PIZZA_MEDIUM)) {
return PRICE_MEDIUM;
} else if (pizzaName.equals(PIZZA_LARGE)) {
return PRICE_LARGE;
} else {
return PRICE_COLOSSAL;
}
}
}
Notice how I corrected the return type from int to double on getDiameter and getBasePrice, as the constants you are trying to return are double. I also fixed the misspelling of "Medium" in some places.
To fix the scanner error, you must import it's package using: (Add this at the top of the file)
import java.util.Scanner;
You main method should look like this: (Again, misspelling variables and casting errors)
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter The Size of Pizza you" + "want: (S/M/L/C)");
String option = keyboard.nextLine().trim().substring(0,1).toUpperCase();
double pizzaPrice;
double pizzaSize;
if(option.equals(PIZZA_SMALL))
{
pizzaPrice = SMALL_DIAMETER;
pizzaSize = SMALL_DIAMETER;
}
else if (option.equals(PIZZA_MEDIUM))
{
pizzaPrice = PRICE_MEDIUM;
pizzaSize = MEDIUM_DIAMETER;
}
else if (option.equals(PIZZA_LARGE))
{
pizzaPrice = PRICE_LARGE;
pizzaSize = LARGE_DIAMETER;
}
else
{
option = PIZZA_COLLOSAL;
pizzaPrice = PRICE_COLOSSAL;
pizzaSize = COLOSSAL_DIAMETER;
}
System.out.println("Pizza Size: " + option);
System.out.println("Enter The Number of Toppings" +
"you want:(0-8)");
int pizzaTopping = keyboard.nextInt();
if(pizzaTopping < MIN_TOPPINGS)
{
pizzaTopping = MIN_TOPPINGS;
}
else if(pizzaTopping > MAX_TOPPINGS)
{
pizzaTopping = MAX_TOPPINGS;
}
double radius = getDiameter(option) / 2;
double squareInches = radius * radius * Math.PI;
System.out.println("Pizza Size: " + option + "( " + pizzaSize +
"inch -- " + squareInches + " square inches)" );
System.out.println("Toppings: " + pizzaTopping);
double priceWithToppings = getBasePrice(option) + pizzaTopping * 9;
System.out.println("Price: " + priceWithToppings);
double pizzaTax = priceWithToppings * PRICE_TAX;
System.out.println("Tax: "+ pizzaTax);
double totalPrice = priceWithToppings + pizzaTax;
System.out.println("Total Price: " + totalPrice);
double priceEachSquareInch = priceWithToppings / squareInches;
System.out.println("Price/sq.in.: " + priceEachSquareInch);
}
}
If you want to cast a double to an int, you need to do int something = (int)myDouble. Also pay attention when writing your variable names, as they must be exactly the same as the definition or they will throw an error. Also, if a method returns an int, but you try and return a double, it will result in an error, as the return type must be the same as what is defined in the method.
Firstly, take a good look through your code as many of your errors are typos - e.g. toUppercase(), COLLOSAL etc.
And, as per Ben's comment you are using doubles for your constants but then your methods are all integers. Java won't let you do this automatically as it results everything after the decimal point being lost as integers are whole numbers only.
When these two things are changed your code appears to work - at a quick glimpse at least.

Categories

Resources