Adblock breaks this site

Something Wrong With My Program

Discussion in 'Programming General' started by R2Pleasent, Oct 19, 2009.

  1. R2Pleasent

    R2Pleasent GGBoost.com - ELO Boosting Service
    Retired Global Moderator $25 USD Donor

    Joined:
    Feb 1, 2007
    Posts:
    13,900
    Referrals:
    108
    Sythe Gold:
    2,528
    Discord Unique ID:
    331126295314563074
    Two Factor Authentication User Verified Challenger Sythe's 10th Anniversary Tier 1 Prizebox Member of the Month Winner
    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);
        
        
    }
     
  2. Jimmy

    Jimmy Ghost
    Retired Sectional Moderator $5 USD Donor

    Joined:
    Jun 24, 2008
    Posts:
    2,421
    Referrals:
    10
    Sythe Gold:
    25
    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);
        }
    
    }
     
  3. Nullware

    Nullware Guru

    Joined:
    Jan 30, 2007
    Posts:
    1,761
    Referrals:
    4
    Sythe Gold:
    0
    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);
        }
    
    }
     
< Need help with my application. | Need Quick Help >


 
 
Adblock breaks this site