The Artima Developer Community
Sponsored Link

Java Answers Forum
illegal start of expression...

3 replies on 1 page. Most recent reply: Nov 23, 2009 7:17 PM by joel tan

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 3 replies on 1 page
joel tan

Posts: 3
Nickname: igottanoe
Registered: Nov, 2009

illegal start of expression... Posted: Nov 22, 2009 8:24 PM
Reply to this message Reply
Advertisement
Hi,
newbie here.
Anyway the program was :

Create class call Room
Have two constructors that accepts parameters width and length.and calculate both the area and the perimeter.

test it out by getting users to input the data and using each of the constructors
to display the results, I had to create a method call displayDimensions

here's my code.
/**
 *Assignment9
 *adding on to assignment 8 Room class 
 *add on : one more constructor + method getPerimeter
 */
public class Room2
{
    private double length;
    private double width;
    
    public Room2 ( )   //No arg- constructor
    {
        length = 0.0;
        width = 0.0;
    }
    
    public Room2 (double lengt, double widt )  // second constructor
    {
        length = lengt;
        width = widt;
 
        // I AM GETTING THE ERROR HERE : ILLEGIAL START OF EXPRESSION
    public void setLength ( double leng) //stores the value for length           
    {
        length = leng;
    }
    
    public void setWidth (double wid) //stores the value of width
    {
        width = wid;
    }
    
    public double getLength( ) //Get length
    {
        return length;
    }
    
    public double getWidth ( ) // Get width
    {
        return width;
    }
    
    public double findArea ( )  // Get area
    {
        return length * width;
    }
    
    public double getPerimeter()  // Get perimeter
    {
        return (length*2) + (width*2);
    }
}

and to the test program
/**
 * Assignment9
 * 
 * program to test class Room2
 */
 
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
 
public class RoomTest2
{
    public static void main (String args)
    {
        double lengt, widt; // for second room
        String input; //user input
        double inp; // changing user input 
        double ar1, ar2; // area
        double pr1, pr2; // Perimeter
        
        Room2 dimension1 = new Room2();
        Room2 dimension2 = new Room2( lengt, widt);
        
        DecimalFormat formatter = new DecimalFormat ("##.00");
        
        // GET LENGTH AND WIDTH OF BOTH ROOMS
        
        //first room
        //width
        input = JOptionPane.showInputDialog (" Enter room 1 width: ");
        inp = Double.parseDouble(input);
        dimension1.setWidth(inp);
        //length
        input = JOptionPane.showInputDialog ("Enter length of Room 1: ");
        inp = Double.parseDouble(input);
        dimension1.setLength(inp);
        //area
        ar1 = dimension1.findArea( );
        //Perimeter
        pr1 = dimension1.getPerimeter( );
        
        //second room using second constructor
        
         //width
        input = JOptionPane.showInputDialog (" Enter room 1 width: ");
        widt = Double.parseDouble(input);
        dimension2.setWidth(widt);
        //length
        input = JOptionPane.showInputDialog ("Enter length of Room 1: ");
        lengt = Double.parseDouble(input);
        dimension2.setLength(lengt);
        //area
        ar2 = dimension2.findArea( );
        //Perimeter
        pr2 = dimension2.getPerimeter( );
        
        // display room 1 results
        displayDimensions( );
        {
            double a = dimension1;
        }
        
        //display room 2 results
        displayDimensions( );
        {
            double a = dimension2;
        }
        
        
        
    } // End main
    
        public static void displayDimensions ( )
        { 
            JOptionPane.showMessageDialog (null, "Width = "+ ((a).getWidth( )) + "Length = " + ((a).getLength( )) + "\n"+ "area =" + ((a).findArea( )) + "\n" + "Perimeter = " + ((a).getPerimeter( )));
        } // End of display Dimensions
} //End CLASS


Thank you.


joel tan

Posts: 3
Nickname: igottanoe
Registered: Nov, 2009

Re: illegal start of expression... Posted: Nov 22, 2009 11:55 PM
Reply to this message Reply
ok I've found the mistake. Edvidently I'm missing a " } "

However I'm getting another error with my test program.

its at this line:
        double a;
        // display room 1 results
        displayDimensions( );
        {
            a = dimension1;
        }


error being : incompatible types -found Room2 but expected double

George B

Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008

Re: illegal start of expression... Posted: Nov 23, 2009 6:10 AM
Reply to this message Reply
I had to fix a few bugs in your test program to get it to work. Modifications are shown below with old code commented out using "//old//" and new code marked "//new//".

1) The parameter for the main() method needs to be an array of String, not just String.

2) You were calling the Room2(double, double) constructor before you had retrieved the length and width values from the user, so you had to set them using setLength() and setWidth() afterward which kind of skips the point of the exercise (if I understand it correctly). I moved the call to the constructor to after the values are collected and eliminated the calls to setLength() and setWidth() for dimension2.

3) Instead of trying to set a variable to be printed by your displayDimensions routine, I modified it to pass in the Room2 object whose dimensions you want to display as a parameter.

4) I commented out a few lines that weren't being used.

/**
 * Assignment9
 * 
 * program to test class Room2
 */
 
import javax.swing.JOptionPane;
import java.text.DecimalFormat;
 
public class RoomTest2
{
    //old// public static void main (String args)
    public static void main (String[] args) //new//
    {
        double lengt, widt; // for second room
        String input; //user input
        double inp; // changing user input 
        double ar1, ar2; // area
        double pr1, pr2; // Perimeter
        
        Room2 dimension1 = new Room2();
        //old// Room2 dimension2 = new Room2( lengt, widt);
        
        DecimalFormat formatter = new DecimalFormat ("##.00");
        
        // GET LENGTH AND WIDTH OF BOTH ROOMS
        
        //first room
        //width
        input = JOptionPane.showInputDialog (" Enter room 1 width: ");
        inp = Double.parseDouble(input);
        dimension1.setWidth(inp);
        //length
        input = JOptionPane.showInputDialog ("Enter length of Room 1: ");
        inp = Double.parseDouble(input);
        dimension1.setLength(inp);
        //area
        //old// ar1 = dimension1.findArea( );
        //Perimeter
        //old// pr1 = dimension1.getPerimeter( );
        
        //second room using second constructor
        
         //width
        input = JOptionPane.showInputDialog (" Enter room 1 width: ");
        widt = Double.parseDouble(input);
        //old// dimension2.setWidth(widt);
        //length
        input = JOptionPane.showInputDialog ("Enter length of Room 1: ");
        lengt = Double.parseDouble(input);
        //old// dimension2.setLength(lengt);
        Room2 dimension2 = new Room2( lengt, widt); //new//
        //area
        //old// ar2 = dimension2.findArea( );
        //Perimeter
        //old// pr2 = dimension2.getPerimeter( );
        
        // display room 1 results
        //old// displayDimensions( );
        //old// {
        //old//     double a = dimension1;
        //old// }
        displayDimensions(dimension1); //new//
        
        //display room 2 results
        //old// displayDimensions( );
        //old// {
        //old//     double a = dimension2;
        //old// }
        displayDimensions(dimension2); //new//
        
        
        
        
    } // End main
    
        //old// public static void displayDimensions ( )
        public static void displayDimensions (Room2 a) //new//
        { 
            JOptionPane.showMessageDialog (null,
                                           "Width = "+ ((a).getWidth( )) +
                                           "Length = " + ((a).getLength( )) + "\n"+
                                           "area =" + ((a).findArea( )) + "\n" +
                                           "Perimeter = " + ((a).getPerimeter( )));
 
        } // End of display Dimensions
} //End CLASS
 

joel tan

Posts: 3
Nickname: igottanoe
Registered: Nov, 2009

Re: illegal start of expression... Posted: Nov 23, 2009 7:17 PM
Reply to this message Reply
Thank you

I understand the whole concept better now.

Flat View: This topic has 3 replies on 1 page
Topic: Novice to Java needs help. Previous Topic   Next Topic Topic: Warning: Unchecked call

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use