2 Replies - 1343 Views - Last Post: 11 April 2007 - 06:53 AM Rate Topic: -----

#1 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Need help with Objects and Constructors

Posted 10 April 2007 - 07:16 PM

Ok, I'm having trouble with this. Seemed to work fine before I started using JOptionPanes. Anyway, I'm supposed to make a rectangle that takes input from the user for color, width and height. I'm supposed to use a constructor (which I did) and I'm supposed to use 2 objects to display this for 2 rectangles (but I haven't got that far because the first one doesn't work quite yet) Here is what I have so far:
import javax.swing.JOptionPane;

public class first {
	// Main Method
	public static void main(String[]args) {
		// Create a rectangle
		Rectangle1 myRectangle = new Rectangle1();
		
		// Ask User for color
		String colorInput = JOptionPane.showInputDialog(
		  "Enter color for the rectangle\n or leave blank and press Enter");
		
		// Convert colorInput string to a double
		double color1 = Double.parseDouble(colorInput);
		
		// Ask User for height
		String heightInput = JOptionPane.showInputDialog(
		   "Enter a number for the height of a Rectangle: ");
   
		// Convert heightInput to a double
		double height1 = Double.parseDouble(heightInput);
		
		// Ask User for width
		String widthInput = JOptionPane.showInputDialog(
			"Enter a number for the width of a Rectangle: ");
		
		// Display Results
		String color = "The color is "
		 + color1 + "\nThe area of the rectangle Rectangle1 is "
		 + myRectangle.getArea() + "\nand the perimeter is "
		 + myRectangle.getPerimeter();
		JOptionPane.showMessageDialog(null, color);
	}
}

class Rectangle1 {
	// Rectangle Class

	double height = 20;
	double width = 50;

	// Construct a rectangle with area of 1
	Rectangle1(){
		double area = height * width;
	}

	// Find perimeter of the rectangle
	double Perimeter = (height * 2) + (width * 2);

	// Return the area of this rectangle
	double getArea(){
		return height * width;
	}

	// Return the Perimeter of this rectangle
	double getPerimeter(){
		return Perimeter;
	}
}



Here's the errors I'm getting:
Warning: Cannot convert string "-b&h-lucida-medium-r-normal-sans-*-140-*-*-p-*-iso8859-1" to type FontStruct
Exception in thread "main" java.lang.NumberFormatException: For input string: "black"
	at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
	at java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1207)
	at java.lang.Double.parseDouble(Double.java:220)
	at first.main(first.java:14)


Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Objects and Constructors

#2 Israel  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 7
  • View blog
  • Posts: 818
  • Joined: 22-November 04

Re: Need help with Objects and Constructors

Posted 10 April 2007 - 09:02 PM

I think I found the solution, I don't know if I have it exactly like my professor wants it but it runs. Here's the solution I have if anyone's curious.

import javax.swing.JOptionPane;

public class first {
	// Main Method
	public static void main(String[]args) {
		// Create a rectangle
		Rectangle1 myRectangle = new Rectangle1();
				
		// Ask User for Color
		String color1 = JOptionPane.showInputDialog(
		"Enter a color for the color of a Rectangle\n" 
		+ "Or leave blank and hit enter for default: ");

		// Display Results
		String color = "The color is of Rectangle1 is " + color1 
		 + "\nThe area of the rectangle Rectangle1 is "
		 + myRectangle.getArea() + "\nand the perimeter is "
		 + myRectangle.getPerimeter();
		JOptionPane.showMessageDialog(null, color);
	}
}

class Rectangle1 {
	// Rectangle Class
	 
	// Ask User for height
	String heightInput = JOptionPane.showInputDialog(
	   "Enter a number for the height of a Rectangle: ");

	// Convert heightInput to a double
	double height1 = Double.parseDouble(heightInput);

	// Ask User for width
		String widthInput = JOptionPane.showInputDialog(
			"Enter a number for the width of a Rectangle: ");

	// Convert width to a double
	double width1 = Double.parseDouble(widthInput);

	double height = height1;
	double width = width1;
		
	// Construct a rectangle with area of 1
	Rectangle1(){
		double area = height * width;
	}

	// Find perimeter of the rectangle
	double Perimeter = (height * 2) + (width * 2);

	// Return the area of this rectangle
	double getArea(){
		return height * width;
	}

	// Return the Perimeter of this rectangle
	double getPerimeter(){
		return Perimeter;
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 Programmist  Icon User is offline

  • CTO
  • member icon

Reputation: 252
  • View blog
  • Posts: 1,833
  • Joined: 02-January 06

Re: Need help with Objects and Constructors

Posted 11 April 2007 - 06:53 AM

Looking at your original code I can tell you that the exception is coming from one of the calls to Double.parseDouble(...);. Anytime you use one of the Number wrapper classes' static conversion methods (e.g. Integer.parseInt) you have to either make sure that you catch (try-catch) NumberFormatException, or make sure the input can be converted to the correct type of Number. So, for a Double any string of the form nn.nn, where n is a decimal between 0 and 9 can be converted to a Double. However, if you have anything else in there (e.g. '*', 'x', '@', etc) that can't be converted to a double, you'll get the exception. So, try either try to convert, catch any exceptions and throw away bad input, or test for numerical input before trying to convert.

This post has been edited by alcdotcom: 11 April 2007 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1