5 Replies - 1379 Views - Last Post: 12 April 2007 - 01:37 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 Set Method

Posted 10 April 2007 - 10:41 PM

I hate to be a post whore, but my problem changed so I started a new thread. I'm now having trouble with changing my code to use the set method. (At least I think that's what I'm supposed to do to have defaults but accept user input too?)

import javax.swing.JOptionPane;

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

	   // Ask User for Color of second Rectangle
		String color2 = JOptionPane.showInputDialog(
		"Enter a color for the second Rectangle\n"
		+ "Or leave blank and hit enter for default: ");

	   if (color1 == ""){
		   
		   String white1 = "white";
		   color1 = white1;
	   }
	   else{
		   color1 = color1;
	   }

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


class Rectangle {
	// Rectangle Class
	 
	// Ask User for height of first rectangle
	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 of first rectangle
		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 = 1;
	double width = 1;
	  
	// Construct a rectangle with area 
	Rectangle(){
		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;
	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: Need help with Set Method

#2 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Need help with Set Method

Posted 11 April 2007 - 03:50 AM

I've made some changes, as I don't think you are using the rectangle class quite as you should, so I've added some class variables to represent the elements of the rectangle. Is this what you mean? You could then change the colour of the rectangle if you call the setColour() method.

Well, I hope that's useful. :)

import javax.swing.JOptionPane;

public class first {
	// Main Method
	public static void main(String[]args) {
		
		Rectangle rectangles[] = new Rectangle[2];
		
		for (int i=0; i<2; i++)
		{
		
		String heightInput = JOptionPane.showInputDialog(
	 	"Enter a number for the height of a Rectangle: ");
	 	double height1 = Double.parseDouble(heightInput);
	 	
	 	String widthInput = JOptionPane.showInputDialog(
		 "Enter a number for the width of a Rectangle: ");
		 double width1 = Double.parseDouble(widthInput);
		 
		 String color1 = JOptionPane.showInputDialog(
		"Enter a color for the Rectangle\n"
		+ "Or leave blank and hit enter for default: ");
		
		 rectangles[i] = new Rectangle(height1, width1, color1);
		 
		}
		
		
		// Create a rectangle
		//Rectangle myRectangle = new Rectangle();
		
		// Create another rectangle
		//Rectangle secondRectangle = new Rectangle();
				
		// Ask User for Color of first Rectangle
		//String color1 = JOptionPane.showInputDialog(
		//"Enter a color for the first Rectangle\n"
		//+ "Or leave blank and hit enter for default: ");

	   // Ask User for Color of second Rectangle
		//String color2 = JOptionPane.showInputDialog(
		//"Enter a color for the second Rectangle\n"
		//+ "Or leave blank and hit enter for default: ");

	   /*if (color1 == ""){
		  
		   String white1 = "white";
		   color1 = white1;
	   }
	   else{
		   color1 = color1;
	   }*/

		// Display Results
		String color = "The color is of the first rectangle is " + rectangles[0].getColour()
		 + "\nThe area of the first rectangle is "
		 + rectangles[0].getArea() + "\nand the perimeter of the first rectangle"
		 + " is " + rectangles[0].getPerimeter();
		JOptionPane.showMessageDialog(null, color);
		
		String secondbox = "The color of the second rectangle is " + rectangles[1].getColour()
		+ "\nThe area of the second rectangle is " + rectangles[1].getArea()
		+ "\nand the perimeter of the second rectangle is "
		+ rectangles[1].getPerimeter();
		JOptionPane.showMessageDialog(null, secondbox);
	System.exit(0);
	}
	
}


class Rectangle {
	// Rectangle Class
	
	double width;
	double height;
	double area;
	double perimeter;
	String colour;
	
	// Ask User for height of first rectangle
	//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 of first rectangle
	   // 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 = 1;
   // double width = 1;
	  
	// Construct a rectangle with area
	Rectangle(double h, double w, String c){
		height = h;
		width = w;
		
		if (c == null || c.length() == 0){
		colour = "white";
		}
		
		else 
		{colour = c;}
		
		area = height * width;
		perimeter = (height * 2) + (width * 2);
	}

	// 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;
	}
	String getColour(){
		return colour;
	}
	void setColour(String c) {
		colour = c;
	}
}

Was This Post Helpful? 0
  • +
  • -

#3 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 Set Method

Posted 11 April 2007 - 07:51 AM

I hate my professor! After working until 2am last night I got to class and he said that I wasn't supposed to take inputs from the user. Even thought his instructions said, and I quote "Use JOptionPane methods to do your inputs and outputs" He also failed to mention that the color is somehow supposed to be linked into the getPerimeter method. So now I gotta re-write this thing from scratch. I'll re-post my new code later after school/work. I just had to vent that. :angry:

This post has been edited by Israel: 11 April 2007 - 07:52 AM

Was This Post Helpful? 0
  • +
  • -

#4 Programmist  Icon User is offline

  • CTO
  • member icon

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

Re: Need help with Set Method

Posted 11 April 2007 - 09:40 AM

Ahhh...the eternal struggle to define/understand the requirements. You're getting an early start. One day you'll be saying the same thing, but in place of "Instructor" you'll be saying "users." :)
Was This Post Helpful? 0
  • +
  • -

#5 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 Set Method

Posted 11 April 2007 - 12:40 PM

Ok, I think this is all I have left on this. (Thanks Ellie!) I just need to set defaults for width and height now too. I've tried this:

		if(h == null || h.length() == 0){
			height = 1;
		}
		
		else
		{height = h;}
		
		if(w == null || w.lenght() == 0){
			width = 1;
		}
		else
		{width = w;}


But I keep getting errors?
Was This Post Helpful? 0
  • +
  • -

#6 Ellie  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 8
  • View blog
  • Posts: 533
  • Joined: 17-January 07

Re: Need help with Set Method

Posted 12 April 2007 - 01:37 AM

The length method is only good for Strings, so you can't use it on your numeric variables. I would maybe use if (w <= 0) to prevent people using zero or negative values for the width/height (testing for null should work ok as you've done though).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1