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 18 of 18

Thread: New to Objects...

  1. #1
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default New to Objects...

    I'm starting on a very simple program dealing with classes and objects and I'm confused about what's going on here...

    Class file

       public class Employee {
          private String fname;
          private String lname;
          private String id;
    		private double salary;
          Employee() {
     
             fname = "--empty--";
             lname = "--empty--";
             id = "--empty--";
             salary = 0;
          }
     
          Employee(String f, String l, String i, String s) {
     
             fname = f;
             lname = l;
             id = i;
             salary = 0;
          }
     
          public void setfname (String f) {
     
             fname = f;
          }
     
          public String getfname() {
     
             return fname;
          }
     
          public void setlname(String l) {
     
             lname=l;
          }
     
          public String getlname() {
     
             return lname;
          }
     
          public void setid(String i) {
     
             id=i;
          }
     
          public String getid() {
     
             return id;
          }
     
          public void setsalary (double s) {
     
             salary = s;
          }
     
          public double getsalary() {
     
             return salary;
          }
       }

    This compiles fine

    Here is the run program

       import javax.swing.*;
     
       public class TestEmployee
       {
          public static void main (String[] args)
          {
             String firstn;
             String lastn;
             String idnum;
             double salary;
     
             firstn = JOptionPane.showInputDialog(null,"Enter First Name");
             lastn = JOptionPane.showInputDialog(null,"Enter Last Name");
             idnum = JOptionPane.showInputDialog(null,"Enter ID Number");
             salary = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Salary"));
     
             Employee firstGuy = new Employee( firstn, lastn, idnum, salary);
     
             firstn=JOptionPane.showInputDialog(null,"Enter First Name");
             lastn=JOptionPane.showInputDialog(null,"Enter Last Name");
             idnum=JOptionPane.showInputDialog(null,"Enter ID Number");
             salary = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Salary"));
     
             Employee secondGuy = new Employee(firstn, lastn, idnum, salary);
     
             JOptionPane.showMessageDialog(null,"Firstname: " + firstGuy.getfname() + 
                "\nLastname: " + firstGuy.getlname() + "\nID number: " + firstGuy.getid() + 
                "\nSalary: " + firstGuy.getsalary());
     
             JOptionPane.showMessageDialog(null,"Firstname: " + secondGuy.getfname() + 
                "\nLastname: " + secondGuy.getlname() + "\nID number: " + secondGuy.getid() +
                "\nSalary: " + secondGuy.getsalary());  
          }
     
       }

    I can't seem to compile this very simple program. It makes me feel silly. Any help would be great. Here are the error messages.

    TestEmployee.java:17: cannot find symbol
    symbol : constructor Employee(java.lang.String,java.lang.String,java.la ng.String,double)
    location: class Employee
    Employee firstGuy = new Employee( firstn, lastn, idnum, salary);
    ^
    TestEmployee.java:24: cannot find symbol
    symbol : constructor Employee(java.lang.String,java.lang.String,java.la ng.String,double)
    location: class Employee
    Employee secondGuy = new Employee(firstn, lastn, idnum, salary);
    ^
    2 errors


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: New to Objects...

    Have a look at the constructor(s) for Employee (takes 4 strings) vs how it is attempted to be used (3 strings and 1 double)

  3. The Following User Says Thank You to copeg For This Useful Post:

    Java Neil (March 22nd, 2011)

  4. #3
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by copeg View Post
    Have a look at the constructor(s) for Employee (takes 4 strings) vs how it is attempted to be used (3 strings and 1 double)
    Ah! Cool.

    Now it compiles but when I run it no values are being stored in the salary field. It just displays 0.0?

    Once I get some of these basics down I should be able to truck along.

  5. #4
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: New to Objects...

    salary = 0;
    You declare salary as 0.
    You should be passing it a value from the constructor (or make use of your setSalary()).
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  6. #5
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by newbie View Post
    salary = 0;
    You declare salary as 0.
    You should be passing it a value from the constructor (or make use of your setSalary()).
    OK, I see what you mean.

    I feel kinda dumb.
    Last edited by Java Neil; March 22nd, 2011 at 09:28 PM.

  7. #6
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: New to Objects...

    What are you doing with the other 3 variables in the constructor?

  8. #7
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by Junky View Post
    What are you doing with the other 3 variables in the constructor?
    They are just strings for input.

    Here is the corrected code

    class code
       public class Employee {
          private String fname;
          private String lname;
          private String id;
    		private double salary;
          Employee() {
     
             fname = "--empty--";
             lname = "--empty--";
             id = "--empty--";
             salary = 0;
          }
     
          Employee(String f, String l, String i, double s) {
     
             fname = f;
             lname = l;
             id = i;
             salary = s;
          }
     
          public void setfname (String f) {
     
             fname = f;
          }
     
          public String getfname() {
     
             return fname;
          }
     
          public void setlname(String l) {
     
             lname = l;
          }
     
          public String getlname() {
     
             return lname;
          }
     
          public void setid(String i) {
     
             id = i;
          }
     
          public String getid() {
     
             return id;
          }
     
          public void setsalary (double s) {
     
             salary = s;
          }
     
          public double getsalary() {
     
             return salary;
          }
       }

    main code with array
       import javax.swing.*;
     
       public class TestEmployeeArray {
     
          public static void main (String[] args) {
     
             String firstn;
             String lastn;
             String idnum;
             double salary;
     
             Employee[] empArray = new Employee[5];
     
             for (int i = 0; i < empArray.length; i++) {
     
                firstn = JOptionPane.showInputDialog(null,"Enter First Name");
                lastn = JOptionPane.showInputDialog(null,"Enter Last Name");
                idnum = JOptionPane.showInputDialog(null,"Enter ID Number");   
                salary = Double.parseDouble(JOptionPane.showInputDialog(null,"Enter Salary"));
     
                empArray[i] = new Employee(firstn, lastn, idnum, salary);
             }
     
             for (int i = 0; i < empArray.length; i++) {
     
                JOptionPane.showMessageDialog(null,"First Name: " + empArray[i].getfname() + "\nLast Name: " 
                + empArray[i].getlname() + "\nID Number: " + empArray[i].getid() + "\nSalary: " + empArray[i].getsalary());   
             }         
             JOptionPane.showMessageDialog(null,"Good Bye");
          }
       }

  9. #8
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    For my hardest assignment in this chapter I am supposed to create a "class Rectangle" to represent a rectangle. The class needs to have the following...

    1. Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both values.
    2. A no-arg constructor that creates a default rectangle.
    3. A constructor that creates a rectangle with the specified width and height.
    4. A method named getArea() that returns the area of this rectangle.
    5. A method named getPerimeter() that returns the perimeter.

    Write a test program that creates two rectangles--one with width 4 and height 40 and the other with width 3.5 and height 35.9. Display the width, height, area, and perimeter of each rectangle in this order.

    My teacher has also said he wants to add a default color - which means also add a setColor method to change the color of one of the rectangles. I'm a bit confused by this one. I should also state that he loves to change his mind and decide he wants user input for the rectangles, so I usually design that way to be covered.

    Anywho, this is my class file so far.

       public class Rectangle {
     
        //The private variables
          private double x;
          private double y;
          private double height;
          private double width;
          private double area;
          private double perimeter;
          private String color;
     
        //default constructor that sets values
          public Rectangle() {
     
             double x = 0;
             double y = 0;
             double height = 1;
             double width = 1;
             double area = 0;
             double perimeter = 0;
             String color = " ";
          }
        //non-default constructor that sets values based on input
          public Rectangle(double x, double y, double width, 
          double height, double area, double perimeter, String color) {
     
             setX(x);
             setY(y);
             setWidth(width);
             setHeight(height);
             setColor(color);
             setArea(area);
             setPerimeter(perimeter);
          }
     
        //method for setting the height of the rectangle
          public void setHeight(double newHeight) {
     
             if (newHeight < 0) {
     
                throw new IllegalArgumentException("Invalid Data Entered!");
             }
             else  {
                height = newHeight;
             }
          }
     
        //method for returning the height of the rectangle
          public double getHeight() {
     
             return height;
          }
     
         //method for setting the width of the rectangle
          public void setWidth(double newWidth) {
     
             if (newWidth < 0) {
     
                throw new IllegalArgumentException("Invalid Data Entered!");
             }
             else {
     
                width = newWidth;
             }
          }
     
        //method for returning the width of the rectangle
          public double getWidth() {
     
             return width;
          }
     
        //method for setting the X coordinate
          public void setX(double newX)	{
     
             if (newX < 0) {
     
                throw new IllegalArgumentException("Invalid Data Entered!");
             }
             else {
     
                x = newX;
             }	     
          }
     
        //method for returning the X coordinate
          public double getX() {
     
             return x;
          }
        //method for setting the X coordinate
          public void setY(double newY)	{
     
             if (newY < 0) {
     
                throw new IllegalArgumentException("Invalid Data Entered!");
             }
             else {
     
                y = newY;
             }
          }
     
        //method for returning the Y coordinate
          public double getY()	{
     
             return y;
          }
     
        //method for setting the Area of the rectangle
          public void setArea(double newArea)	{
     
             if (newArea < 0) {
     
                throw new IllegalArgumentException("Invalid Data Entered!");
             }
             else {
     
                area = newArea;
             }
          }
     
        //method for setting the Area of the rectangle
          public double getArea() {
     
             return area;
          }
     
        //method for setting the Area of the rectangle
          public void setPerimeter(double newPerimeter) {
     
             if (newPerimeter < 0) {
     
                throw new IllegalArgumentException("Invalid Data Entered!");
             }
             else {
     
                perimeter = newPerimeter;
             }
          }
          public double getPerimeter() {
     
             return perimeter;
          }  
          public void setColor(String color) {
     
             color = " ";
          }
     
        //method for returning the Color
          public String getColor() {
     
             return color;
          }
     
        //boolean method for comparing rectangles and seeing if the values are equal
          public boolean equals(Rectangle o) {
     
             return (x == o.x
                && y == o.y
                && width == o.width
                && height == o.height); 
          }
     
       }

    Could you guys look over my code and point out my logic errors. I could also use as much advice as possible pertaining to both this class file and the strategy for designing the test program.

    Thanks a lot for your help.

  10. #9
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Well after about 3 hours, I need some help with something I haven't learned yet.

    Here is my first attempt at this program. My main goal was just to have it compile and show me the fields.

       import javax.swing.*;
     
       public class TestRectangle {
     
          public static void main (String[] args) {
     
             Rectangle rectangle1 = new Rectangle();
             rectangle1.setHeight1(Rectangle.height1);
             rectangle1.setWidth1(Rectangle.width1);
             rectangle1.setArea(Rectangle.area);
             rectangle1.setPerimeter(Rectangle.perimeter);
             JOptionPane.showMessageDialog(null, Rectangle.toString());
     
          }
       } 
     
    //Class file
       class Rectangle {
     
          public static int height1 = 4;
          public static int width1 = 40;
          public static double height2 = 1;
          public static double width2 = 1;
          public static double area = 0;
          public static double perimeter = 0;
          String color = "Red";
     
          public Rectangle() {
          }
     
        //First Rectangle  
          public int getHeight1(){
             return height1;
          }
     
          public void setHeight1(int newHeight1) {
             height1 = newHeight1;
          }
     
          public int getWidth1(){
             return width1;
          }
     
          public void setWidth1(int newWidth1) {
             width1 = newWidth1;
          }
     
        //Second Rectangle
          public double getHeight2(){
             return height2;
          }
     
          public void setHeight2(double newHeight2) {
             height2 = newHeight2;
          }
     
          public double getWidth2(){
             return width2;
          }
     
          public void setWidth2(double newWidth2) {
             width2 = newWidth2;
          }
     
        //Area and Perimeter
          public double getArea(){
             return area;
          }
     
          public void setArea(double newArea) {
             area = newArea;
          }
     
          public double getPerimeter(){
             return perimeter;
          }
     
          public void setPerimeter(double newPerimeter) {
             perimeter = newPerimeter;
          }
     
        //Color for Triangle
          public String getColor() {
             return color;
          }
     
          public void setColor(String newColor) {
             color = newColor;
          }
     
          public String toString() {
             return "Height = " + height1 + "\n"
                + "Width = " + width1 + "\n"
                + "Area = " + area + "\n"
                + "Perimeter = " + perimeter + "\n"
                + "Color = " + color;
          }
       }

    TestRectangle.java:12: non-static method toString() cannot be referenced from a static context
    JOptionPane.showMessageDialog(null, Rectangle.toString());
    ^
    1 error

    ----jGRASP wedge2: exit code for process is 1.

    Can someone straighten me out. I am loosing hair fast!

  11. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: New to Objects...

    Why have you changed all the variables to static. Do you understand what static means? Static variables belongs to the class and not the object. All objects you create will have the same value. For example if a Person's name was static then every Person in the world would be called Fred.

  12. #11
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by Junky View Post
    Why have you changed all the variables to static. Do you understand what static means? Static variables belongs to the class and not the object. All objects you create will have the same value. For example if a Person's name was static then every Person in the world would be called Fred.
    OK, I changed the variables to just public.

       import javax.swing.*;
     
       public class TestRectangle {
     
          public static void main (String[] args) {
     
             Rectangle rectangle1 = new Rectangle();
             rectangle1.setHeight1(Rectangle.height1);
             rectangle1.setWidth1(Rectangle.width1);
             rectangle1.setArea(Rectangle.area);
             rectangle1.setPerimeter(Rectangle.perimeter);
             JOptionPane.showMessageDialog(null, Rectangle.toString());
     
          }
       } 
     
    //Class file
       class Rectangle {
     
          public int height1 = 4;
          public int width1 = 40;
          public double height2 = 1;
          public double width2 = 1;
          public double area = 0;
          public double perimeter = 0;
          String color = "Red";
     
          public Rectangle() {
          }
     
        //First Rectangle  
          public int getHeight1(){
             return height1;
          }
     
          public void setHeight1(int newHeight1) {
             height1 = newHeight1;
          }
     
          public int getWidth1(){
             return width1;
          }
     
          public void setWidth1(int newWidth1) {
             width1 = newWidth1;
          }
     
        //Second Rectangle
          public double getHeight2(){
             return height2;
          }
     
          public void setHeight2(double newHeight2) {
             height2 = newHeight2;
          }
     
          public double getWidth2(){
             return width2;
          }
     
          public void setWidth2(double newWidth2) {
             width2 = newWidth2;
          }
     
        //Area and Perimeter
          public double getArea(){
             return area;
          }
     
          public void setArea(double newArea) {
             area = newArea;
          }
     
          public double getPerimeter(){
             return perimeter;
          }
     
          public void setPerimeter(double newPerimeter) {
             perimeter = newPerimeter;
          }
     
        //Color for Triangle
          public String getColor() {
             return color;
          }
     
          public void setColor(String newColor) {
             color = newColor;
          }
     
          public String toString() {
             return "Height = " + height1 + "\n"
                + "Width = " + width1 + "\n"
                + "Area = " + area + "\n"
                + "Perimeter = " + perimeter + "\n"
                + "Color = " + color;
          }
       }

    TestRectangle.java:8: non-static variable height1 cannot be referenced from a static context
    rectangle1.setHeight1(Rectangle.height1);
    ^
    TestRectangle.java:9: non-static variable width1 cannot be referenced from a static context
    rectangle1.setWidth1(Rectangle.width1);
    ^
    TestRectangle.java:10: non-static variable area cannot be referenced from a static context
    rectangle1.setArea(Rectangle.area);
    ^
    TestRectangle.java:11: non-static variable perimeter cannot be referenced from a static context
    rectangle1.setPerimeter(Rectangle.perimeter);
    ^
    TestRectangle.java:12: non-static method toString() cannot be referenced from a static context
    JOptionPane.showMessageDialog(null, Rectangle.toString());
    ^
    5 errors

    What next? I'm seriously lost?

  13. #12
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: New to Objects...

    How about reading your instructions.

    "Write a test program that creates two rectangles--one with width 4 and height 40 and the other with width 3.5 and height 35.9."

    Rectangle rec1 = new Rectangle(4.0, 40.0);
    Let see if you can go from there.

  14. #13
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by Junky View Post
    How about reading your instructions.

    "Write a test program that creates two rectangles--one with width 4 and height 40 and the other with width 3.5 and height 35.9."

    Rectangle rec1 = new Rectangle(4.0, 40.0);
    Let see if you can go from there.
    Like I said before. My teacher has only covered about 30 minutes worth of material for the last part of the last class. I have not grasped the material fully yet.

    I applied you code suggestion here

       import javax.swing.*;
     
       public class TestRectangle {
     
          public static void main (String[] args) {
     
             Rectangle rectangle1 = new Rectangle(4, 40, 3.5, 35.9);
             rectangle1.setHeight1(Rectangle.height1);
             rectangle1.setWidth1(Rectangle.width1);
             rectangle1.setArea(Rectangle.area);
             rectangle1.setPerimeter(Rectangle.perimeter);
             JOptionPane.showMessageDialog(null, Rectangle.toString());
     
          }
       } 
     
    //Class file
       class Rectangle {
     
          public int height1 = 4;
          public int width1 = 40;
          public double height2 = 1;
          public double width2 = 1;
          public double area = 0;
          public double perimeter = 0;
          String color = "Red";
     
          public Rectangle() {
          }
     
        //First Rectangle  
          public int getHeight1(){
             return height1;
          }
     
          public void setHeight1(int newHeight1) {
             height1 = newHeight1;
          }
     
          public int getWidth1(){
             return width1;
          }
     
          public void setWidth1(int newWidth1) {
             width1 = newWidth1;
          }
     
        //Second Rectangle
          public double getHeight2(){
             return height2;
          }
     
          public void setHeight2(double newHeight2) {
             height2 = newHeight2;
          }
     
          public double getWidth2(){
             return width2;
          }
     
          public void setWidth2(double newWidth2) {
             width2 = newWidth2;
          }
     
        //Area and Perimeter
          public double getArea(){
             return area;
          }
     
          public void setArea(double newArea) {
             area = newArea;
          }
     
          public double getPerimeter(){
             return perimeter;
          }
     
          public void setPerimeter(double newPerimeter) {
             perimeter = newPerimeter;
          }
     
        //Color for Triangle
          public String getColor() {
             return color;
          }
     
          public void setColor(String newColor) {
             color = newColor;
          }
     
          public String toString() {
             return "Height = " + height1 + "\n"
                + "Width = " + width1 + "\n"
                + "Area = " + area + "\n"
                + "Perimeter = " + perimeter + "\n"
                + "Color = " + color;
          }
       }

    TestRectangle.java:7: cannot find symbol
    symbol : constructor Rectangle(int,int)
    location: class Rectangle
    Rectangle rectangle1 = new Rectangle(4, 40);
    ^
    TestRectangle.java:8: non-static variable height1 cannot be referenced from a static context
    rectangle1.setHeight1(Rectangle.height1);
    ^
    TestRectangle.java:9: non-static variable width1 cannot be referenced from a static context
    rectangle1.setWidth1(Rectangle.width1);
    ^
    TestRectangle.java:10: non-static variable area cannot be referenced from a static context
    rectangle1.setArea(Rectangle.area);
    ^
    TestRectangle.java:11: non-static variable perimeter cannot be referenced from a static context
    rectangle1.setPerimeter(Rectangle.perimeter);
    ^
    TestRectangle.java:12: non-static method toString() cannot be referenced from a static context
    JOptionPane.showMessageDialog(null, Rectangle.toString());
    ^
    6 errors

    I just need to see the proper form one time and I'll have it.

  15. #14
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: New to Objects...

    NOOOooooo!

    Your instance variables should either have no values or their default values. You use the constructor to set their values with the values passed as parameters. Then there is no need to make all those calls to the setXXX methods. The area and perimeter should be calculated from the height and width values that you create the Rectangle object with.

  16. #15
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by Junky View Post
    NOOOooooo!

    Your instance variables should either have no values or their default values. You use the constructor to set their values with the values passed as parameters. Then there is no need to make all those calls to the setXXX methods. The area and perimeter should be calculated from the height and width values that you create the Rectangle object with.
    OK, I don't understand. As I said I work better with "seeing the logic" rather than being told. Never built an object . No nothing of how constructors work. You're naming parts of an object that I have just started hearing about.

    Even my teacher shows me examples of the proper syntax so we can start seeing through the code and get to the heart of the matter.

  17. #16
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: New to Objects...

    Rectangle rec1 = new Rectangle(4.0, 40.0);
    Rectangle rec2 = new Rectangle(3.5, 35.9);
     
    class Rectangle {
        private double height;
        private double width;
     
        public Rectangle(double w, double h) {
            width = w;
            height - h;
        }
     
        // other constructors and methods
    }
    It really isn't that difficult. Sorry to be tough but if you are struggling with this you are in for a world of hurt over the rest of the course.

  18. #17
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Quote Originally Posted by Junky View Post
    Rectangle rec1 = new Rectangle(4.0, 40.0);
    Rectangle rec2 = new Rectangle(3.5, 35.9);
     
    class Rectangle {
        private double height;
        private double width;
     
        public Rectangle(double w, double h) {
            width = w;
            height - h;
        }
     
        // other constructors and methods
    }
    It really isn't that difficult. Sorry to be tough but if you are struggling with this you are in for a world of hurt over the rest of the course.
    Now see...That was very helpful! Thanks for setting up the form.

    I'll get it. I just started objects. No need to doom me before I apply the concepts.
    Last edited by Java Neil; March 24th, 2011 at 12:01 AM.

  19. #18
    Member Java Neil's Avatar
    Join Date
    Jan 2011
    Posts
    72
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: New to Objects...

    Check me...

    I have two codes that both work. One code based off of the example in my book, and the example you gave me.

    Book example:
       import javax.swing.JOptionPane;
       public class TestRectangle {
          public static void main(String[] args) {
     
             Rectangle rectangle = new Rectangle();
     
             rectangle.setHeight(40);
             rectangle.setWidth(4);
             rectangle.setArea(0);
             rectangle.setColor("Yellow");
     
             JOptionPane.showMessageDialog(null, rectangle.toString());
     
             Rectangle rectangle2 = new Rectangle();
     
             rectangle2.setHeight2(35.9);
             rectangle2.setWidth2(3.5);
             rectangle2.setArea(0);
             rectangle2.setColor("Blue");
     
             JOptionPane.showMessageDialog(null, rectangle2.toString2());
          }
       }
     
       class Rectangle {
     
          private int height;
          private int width;
          private double height2;
          private double width2;
          private double area;
          private double perimeter;
          private String color = "white";
     
          public Rectangle() {
          }
     
       	//Int Rectangle
          public int getHeight() {
             return height;
          }
     
          public void setHeight(int newHeight) {
             height = newHeight;
          }
     
          public int getWidth() {
             return width;
          }
     
          public void setWidth(int newWidth) {
             width = newWidth;
          }
     
          //Double Rectangle
          public double getHeight2() {
             return height2;
          }
     
          public void setHeight2(double newHeight2) {
             height2 = newHeight2;
          }
     
          public double getWidth2() {
             return width2;
          }
     
          public void setWidth2(double newWidth2) {
             width2 = newWidth2;
          }
     
          public double getArea(double width, double height) {
             return area;
          }
     
          public void setArea(double newArea) {
             area = newArea;
          }
     
          public double getPerimeter() {
             return perimeter;
          }
     
          public void setPerimeter(double newPerimeter) {
             perimeter = newPerimeter;
          }
     
     
          public String getColor() {
             return color;
          }
     
          public void setColor(String newColor) {
             color = newColor;
          }
     
          public String toString() {
             return "Rectangle # 1\n" + "\n" 
                + "Height = " + height + "\n"
                + "Width = " + width + "\n"
                + "Area = " + (width * height) + "\n"
                + "Perimeter = " + (2 * width + 2 * height) + "\n"
                + "Color = " + color;
          }
     
          public String toString2() {
             return "Rectangle # 2\n" + "\n"  
                + "Height " + height2 + "\n"
                + "Width " + width2 + "\n"
                + "Area " + (width2 * height2) + "\n"
                + "Perimeter " + (2 * width2 + 2 * height2) + "\n"
                + "Color " + color;
          }
       }

    Junky example
       import javax.swing.JOptionPane;
       public class TestRectangle {
          public static void main(String[] args) {
     
             Rectangle rec1 = new Rectangle(4, 40);
     
             JOptionPane.showMessageDialog(null, rec1.toString());
     
             Rectangle rec2 = new Rectangle(3.5, 35.9);
     
          	JOptionPane.showMessageDialog(null, rec2.toString2());
     
          }
       }
     
       class Rectangle {
     
          private int height;
          private int width;
          private int area;
       	private int perimeter;
       	private String color;
          private double height2;
          private double width2;
          private double area2;
          private double perimeter2;
          private String color2;
     
          public Rectangle(int w, int h) {
     
             width = w;
             height = h;
             area = (h * w);
             perimeter = (2 * h + 2 * w);
             color = "Red";
     
          }
     
          public Rectangle(double w2, double h2) {
     
             width2 = w2;
             height2 = h2;
             area2 = (h2 * w2);
             perimeter2 = (2 * h2 + 2 * w2);
             color2 = "Yellow";
     
          }
     
          public String toString() {
     
             return "Rectangle # 1\n" + "\n" 
                + "Height = " + height + "\n"
                + "Width = " + width + "\n"
                + "Area = " + area + "\n"
                + "Perimeter = " + perimeter + "\n"
                + "Color = " + color;
     
          }
     
          public String toString2() {
     
             return "Rectangle # 2\n" + "\n"  
                + "Height " + height2 + "\n"
                + "Width " + width2 + "\n"
                + "Area " + area2 + "\n"
                + "Perimeter " + perimeter2 + "\n"
                + "Color " + color2;
     
          }
       }

    Please critique both sets of codes if you have time. I would love to go into class today ahead of the game for a change.

Similar Threads

  1. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 02:19 PM
  2. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  3. Help with background and objects
    By Afromiffo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2010, 01:19 AM
  4. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 11:50 AM
  5. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM