George B
Posts: 24
Nickname: hmgeorge
Registered: Feb, 2008
|
|
Re: illegal start of expression...
|
Posted: Nov 23, 2009 6:10 AM
|
|
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
|
|