• Log in or sign up for an account here: https://www.sythe.org/login

Something Wrong With My Program

R2Pleasent

GGBoost.com - ELO Boosting Service

Retired Global Moderator
$25 USD Donor
Joined
Feb 1, 2007
Sythe Gold
2,528
Messages
13,900
Location
Canada!
Two Factor Authentication User
Member of the Month Winner
Sythe Awards 2014 Winner
Sythe's 10th Anniversary
Tier 1 Prizebox
Verified Challenger
Something Wrong With My Program
Here is my Constructor, compiles fine...

Code:
class Ride {
	String name;
	int ticketsRequired;
	float heightRequired;
	int numberOfRiders;
	
	Ride(String n, int tR, float hR, int numR){
		this.name = n;
		this.ticketsRequired = tR;
		this.heightRequired = hR;
		this.numberOfRiders = numR;
	}
	
	Ride(String n, int tR, float hR){
		this.name = n;
		this.ticketsRequired = tR;
		this.heightRequired = hR;
		this.numberOfRiders = 0;
	}
		
 	Ride(){
		this.name = "UNKNOWN";
		this.ticketsRequired = 0;
		this.heightRequired = 0f;
		this.numberOfRiders = 0;
	}
	public String toString() {
		return (this.name + " requiring " + this.ticketsRequired + " with a height restriction of "+this.heightRequired+"'");
	}

}


However, when I test it out, it refuses to work. Says identifier expected on each of my new Ride objects. Anyone know what I'm doing wrong?:

Code:
class RideTestProgram {
	
	Ride r1, r2;

    r1 = new Ride("Super Charger", 2, 3f);
    r2 = new Ride("Mastermind", 3, 10f);
    
    System.out.println(r1);
    System.out.println(r2);
    
    
}
 
Something Wrong With My Program
Your assignments and method calls must be put in a method- they can't just be out in the open like that (unless you are declaring a non-local field, in which case you can provide an assignment, but only as an initalizer in the same statment in which you declare it).

Try something along the lines of:

Code:
class RideTestProgram {

    Ride r1 = new Ride("Super Charger", 2, 3f);
    Ride r2 = new Ride("Mastermind", 3, 10f);

    public static void aMethodExample() {
	System.out.println(r1);
	System.out.println(r2);
    }

}
 
Something Wrong With My Program
Should be using a main method.

Code:
class RideTestProgram {

    public static void main(String[] args) {
        Ride r1, r2;

        r1 = new Ride("Super Charger", 2, 3f);
        r2 = new Ride("Mastermind", 3, 10f);

        System.out.println(r1);
        System.out.println(r2);
    }

}
 
Runescape Gamblingrunescape gambling
Back
Top