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: object oriented program help!!!

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    6
    My Mood
    Worried
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile object oriented program help!!!

    client program
    so basically these is it,
    it is suppost to first

    create an array of type string as the one I did
    then create 10 rectangles with random height and width and color ( the color I couldnīt figure it out yet, but I am getting ther noe)

    then print the number of rectangles created which basically does but, in a road, which is not good, but nvm I canfix that.

    the PROBLEM I truly need help with is I am trying to let the code print all the rectangles and including their areas and perimeters

    however I try to do it, but I believe I am doing something wrong because I debugged it and it ends it at the very first try it is testing it, also I am nt sure whether If I am suppost to use the for loop like that, because I am printing the rectangles previwesly created.

    also I am required to print the information of the rectangle with the largest area, but I am not sure how to

    these is my class and then goes my client program follow by the current output


    THANK YOU!!!! honeslty I am really new at these and really appreciate some help.

     	import java.util.*;
     
    	public class Rectangle{
     	 private double width, height;
     
    	 private static String color ="white";
     
    	 private Date date;
     
    	 private  static int rectangleCreated = 0; 
     
    	 Rectangle(){ // no arg constructor that  creates a default rectangle
     
    		width =1;
    		height =1;
     
    	 rectangleCreated++;
     
    	 date: new Date();
     
    	 }
     
     
    	 Rectangle( double w, double h, String c ){ // CONSTRUCTOR
     
    	 	width = w;
     
    		height = h;
     
    		color = c;
     
    		date = new Date();
     
    		rectangleCreated++;
     
     
    		}
     
    	public static int rectangleCreated(){ 	// a method that returns number of rectangle created 
     
     
    	 return rectangleCreated;
     
    	 } 
     
     
     
    	public double getHeight(){
     
    		return height;
     
    		}
    	public void setHeight ( double h) {
     
    		height = h;
     
    		}
    	public void setWidth ( double w) {
     
    		width = w;
    		}
     
    	public static String getColor(){
     
    		return color;
    		}
     
    	public static void setColor(String c){
    		color = c;
     
    		}
    	public Date getDate(){
     
    		return date;
     
    		}
    	public void setDate ( Date d){
     
    		date = d;
     
    				}
     
     
    	public double getArea(){ // returns area
     
    			return width*height;
     
    		}
    	public double getPerimeter(){ // returns perimeter
     
    		return (2*width* height);
     
    		}
    	public String toString(){
     
    		String S;
    		S = "Rectangle width of" + width;
     
    		S =  S + "and height of" +height;
     
    		S = S + " was created on " + date.toString();
     
    		return S;
     
    			}
    				}




    import java.util.*; 
     
     
     
    public class TestRectangle{
     
    	public static void main (String[]args)
    	{
     
     String[] colors = {"White","Blue","Yellow","Red","Green"};
     
     
     Rectangle[] array = new Rectangle[10]; // rectangle of 10
     
      for(int i = 0; i < 10; i++) {
      	 Rectangle r = new Rectangle();
     
     
    	 r.setWidth((Math.random()*40)+10); 
    	 r.setHeight((Math.random()*40)+10);
     
    	// colors[r.nextString(colors.length)];
     
     
    	// print number of rectangles created
    	System.out.println(Rectangle.rectangleCreated());
    		}	 
    	 // area of rectangles created
     
     
     
     for(int i = 0; i < 10; i++) {
      	 Rectangle r = new Rectangle();
     
    	 System.out.println( r.toString()+ "has area of " + r.getArea()); //" and perimeter of" + r.getPerimeter() );
     
     
    					}
     
    		// g through the array and find the information of a rectangle with largest area.
     
     
    			  }
     
    	   }



    OUTPUT


    [COD ----jGRASP exec: java TestRectangle

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    Exception in thread "main" java.lang.NullPointerException
    at Rectangle.toString(Rectangle.java:101)
    at TestRectangle.main(TestRectangle.java:35)

    ----jGRASP wedge2: exit code for process is 1.
    ----jGRASP: operation complete.
    E][/CODE]


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: object oriented program help!!!

    Quote Originally Posted by Username View Post
    Exception in thread "main" java.lang.NullPointerException
    at Rectangle.toString(Rectangle.java:101)
    at TestRectangle.main(TestRectangle.java:35)
    Search java.lang.NullPointerException for details on what causes an exception of this specific type.

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    25
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: object oriented program help!!!

    hi

    You again creating the object of rectangle and trying to find the area.
    Instead in the same for loop while getting the no of rectangles created, get the area and perimeter.
    System.out.println(Rectangle.rectangleCreated()+"h as area of " + r.getArea() + "and perimeter of" + r.getPerimeter() );

Similar Threads

  1. Object Oriented Programming
    By calebite207 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 17th, 2012, 12:27 PM
  2. NEED HELP TO FIX Object oriented Program Facebook
    By Ryo in forum Object Oriented Programming
    Replies: 15
    Last Post: February 29th, 2012, 10:04 AM
  3. Object oriented programming
    By jonnitwo in forum Object Oriented Programming
    Replies: 8
    Last Post: September 2nd, 2011, 12:18 PM
  4. Object-oriented applet
    By mjpam in forum Object Oriented Programming
    Replies: 26
    Last Post: September 15th, 2010, 06:43 AM
  5. Object Oriented program, no output
    By boardbreaker in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 17th, 2009, 11:11 PM