Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: " Illegal start of expression" Error.

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default " Illegal start of expression" Error.

    This is what my code is about(Homework question):

    1. Define a class named Payment that contains an instance variable of type double that stores the amount of payment and appropriate accessor and mutator methods. Also create a method named PaymentDetails that outputs an english sentence to describe the amount of payment.

    2. Define a class named CashPayment that is derived from Payment. This class should redefine the paymentDetails method to indicate that the payment is in cash. Include appropriate constructor.

    3. Define a class named CreditCardPayment that is derived from Payment. This class should contain instance variables for the name on the card, expiration date, and credit card number. Finally, redefine the paymentDetails method to include all credit card information in the print out.

    Create a main method that creates at least two CashPayment and two CreditCardPayment objects with different values and calls paymentDetails for each.

    I'm completely new to java so I'll sound like a newbie.

    Here's my code:

    class Payment
    {
    	private double amount;
    	public Payment(){	
    	       amount = 0;
    	}
    	public Payment(double amount){
    	        this.amount = amount;
    	}
    	public void setPayment(double amount){
    	         this.amount = amount;
    	}
    	public double getPayment(){
    	         return amount;
    	}
    	public void paymentDetails(){
    	         System.out.println("The payment amount is " + amount);
    	}
    }
    class CashPayment extends Payment{
    	public CashPayment(){
    	       super();
    	}
    	public CashPayment(double amt){
    	       super(amt);
    	}
              public void paymentDetails(){
    	       System.out.println("The cash payment amount is " 
                        + getPayment());
    	}
    }
    class CreditCardPayment extends Payment{
    	private String name;
    	private String expiration;
    	private String creditcard;
    	public CreditCardPayment(){
    		super();
    		name = "";
    		expiration = "";
    		creditcard = "";
    	}
    	public CreditCardPayment(double amt, String name, String expiration, String creditcard){
    		super(amt);
    		this.name = name;
    		this.expiration = expiration;
    		this.creditcard = creditcard;
    	}
              public void paymentDetails(){
    		System.out.println("The credit card payment amount is " + getPayment());
    		System.out.println("The name on the card is: " + name);
    		System.out.println("The expiration date is: " + expiration);
    		System.out.println("The credit card number is: " + creditcard);
    	}
    }
    class Question1Payment{
    	public static void main(String[] args)
    	{
    	  CashPayment cash1 = new CashPayment(50.5), cash2 = new CashPayment(20.45);
    	  CreditCardPayment credit1 = new CreditCardPayment(10.5, "Fred", "10/5/2010", "123456789");
    	  CreditCardPayment credit2 = new CreditCardPayment(100, "Barney", "11/15/2009", "987654321");
    	  System.out.println("Cash 1 details:");
    	  cash1.paymentDetails();
    	  System.out.println();
    	  System.out.println("Cash 2 details:");
    	  cash2.paymentDetails();
    	  System.out.println();
    	  System.out.println("Credit 1 details:");
    	  credit1.paymentDetails();
    	  System.out.println();
    	  System.out.println("Credit 2 details:");
    	  credit2.paymentDetails();
    	  System.out.println();
    	}
    }

    I have a lot of errors that I can't figure out:

    ddddd.java:13: illegal start of expression
    public double getPayment(){
    ^
    ddddd.java:13: ';' expected
    public double getPayment(){
    ^
    ddddd.java:16: illegal start of expression
    public void paymentDetails(){
    ^
    ddddd.java:16: illegal start of expression
    public void paymentDetails(){
    ^
    ddddd.java:16: ';' expected
    public void paymentDetails(){


    ddddd.java:7: illegal start of expression
    public Payment(double amount){
    ^
    ddddd.java:7: '.class' expected
    public Payment(double amount){
    ^
    ddddd.java:7: ';' expected
    public Payment(double amount){
    ^
    ddddd.java:10: illegal start of expression
    public void setPayment(double amount){
    ^
    ddddd.java:10: illegal start of expression
    public void setPayment(double amount){
    ^
    ddddd.java:10: ';' expected
    public void setPayment(double amount){
    ^
    ddddd.java:10: ';' expected
    public void setPayment(double amount){
    ^
    ddddd.java:13: illegal start of expression
    public double getPayment(){
    ^
    ddddd.java:13: ';' expected
    public double getPayment(){
    ^
    ddddd.java:73: reached end of file while parsing
    }}
    ^
    13 errors


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: " Illegal start of expression" Error.

    I'm guessing it might be a bracket error or a missing ; or something.

    Am checking it out now.

  3. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Re: " Illegal start of expression" Error.

    First part compiles. Try putting each class as a separate .java file.

    Yep, you must have all of them in save .java file.

    If each class is in separate file, it compiles fine for me.
    Last edited by javapenguin; February 21st, 2011 at 07:11 PM.

Similar Threads

  1. illegal start of expression error!!!!
    By aks.1393 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 14th, 2011, 07:47 AM
  2. Replies: 6
    Last Post: November 12th, 2010, 03:40 AM
  3. Replies: 1
    Last Post: March 31st, 2010, 09:42 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 07:35 AM
  5. Illegal Start of Expression Error. Any help is appreciated.
    By SKhoujinian91 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 7th, 2009, 11:57 PM