Results 1 to 10 of 10
  1. #1
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Need advice on classes!

    I have an homework assignment with the following guidelines ( please don't spoon feed the answer, I would just like to understand how to do things) :

    Part 1:

    You are required to design and implement the CellPhone class according to the following
    specifications:

    - Upon the creation of a cell phone object, the object must immediately be initialized with
    valid values; that is brand, serial number and price.

    - The design should allow enough flexibility so that the value of any of these attributes can
    be modified later on. For example, it should be possible to create a cell phone object with
    a given price then change its price later on.

    - The design should allow the user of the class to obtain the value of any of the attributes
    individually. The design should also allow the user to view all cell phone information at
    once by passing cell phone objects to the print/println() methods;

    - The design should allow for one cell phone to be compared to another cell phone for
    equality. Two cell phone objects are considered equal if they have the same brand and the
    same price.

    Part 2:

    In that part, you are required to write a public class called UtilizeCellPhones, which is going to
    utilize the CellPhone class that you have created in part1. In the main method of this class, you
    should perform the following operations:

    1) Create three cell phone objects; notice that you must assign a brand, serial number (you
    can initialize it as any 9-digit number) and a price to each of these objects upon creation.

    2) Show all information of each of the three objects.

    3) Change the price of the first object; the price and the brand of the second object, and the
    serial number value of the third object.

    4) Display only the modified attributes of the three objects. All outputs showing only the
    price of a cell phone (i.e. not the rest of the attributes) must be formatted.

    5) Compare some of these cell phone objects for equality and display whether or not they
    are equal.
    and this is what i have right now:

    Java Code:
     class Cellphone {
    	 
    	 String brand = "Samsung" ;
    	 long serialNumber =  000-000-000;
    	 double price = 500;
    	 
    	 public String getBrand()
    		{
    		     
    		     
    			return brand;
    		}
    
    		public void setBrand(String cellphoneBrand)
    		{
    		     
    		     
    			brand = cellphoneBrand;
    		}
    
    		
    		public long getSerialNumber()
    		{
    		     
    		     
    			return serialNumber;
    		}
    
    		
    		public void setSerialNumber(long SN)
    		{
    		     // Sets the price of the vehicle
    		     
    			serialNumber = SN;
    		}
    
    		
    		public double getPrice()
    		{
    		     
    			return price;
    		}
    
    		public void setPrice(double pr)
    		{
    		     // Sets the maximum speed of the vehicle
    		     
    			price = pr;
    		}
    		public void showInfo()
    		{
    		    // Displays vehicle information
    		    System.out.println( brand  + price + serialNumber);
    
    		}
     }
    public class UtilizeCellPhones {
    
    	public static void main(String[] args) {
    		// TODO Auto-generated method stub
          Cellphone cell_1 = new Cellphone();
          Cellphone cell_2 = new Cellphone();
          Cellphone cell_3 = new Cellphone();
          
       
    	   String br = null;
    	cell_1.setBrand(br);
           cell_1.setPrice(565.50);
           cell_1.setSerialNumber(123-456-789);
           
           String Lenovo = lenovo;
    	cell_2.setBrand(Lenovo);
           cell_2.setPrice(500.50);
           cell_2.setSerialNumber(012-456-783);
           
           String HTC1 = HTC;
    	cell_3.setBrand(HTC1);
           cell_3.setPrice(700.50);
           cell_3.setSerialNumber(987654321);
           
          cell_3.showInfo();
           
           
    	}
    
    }
    My questions are as follows:

    1) Did I create a constructor? I can't seem to make the difference between a method and constructor

    2) The fact that I put everything public will allow me to change the values later on as the guidelines suggest, right?

    3) Why can I not set the string to whatever I want in my main class ( I.e: cell_1.setBrand(HTC);will not work)

    4) This is more of a main question toward classes.... why is setting a variable public bad? I know it can be changed later on, but it can only be changed through the source code, not the console, anyway that's what I understand

    Thanks for the help in advance!!

    -Marc

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Need advice on classes!

    Did I create a constructor?
    No.

    A constructor looks a lot like a method, but (1) It has the same name as the class, and (2) It does not have a return type, not even void.

    In your code you give some bogus values to brand, serialNumber, and price. Giving values to those variables is what the constructor should do. (That's the first point in the instructions). So keep the variables but remove the assignment of values to them and have a go at adding a constructor.

    The fact that I put everything public will allow me to change the values later on as the guidelines suggest, right?
    What allows you to be able to set the values later is that each of them has an associated setter method: setBrand() etc. Those methods don't need the variables to be public in order to function because they are part of the class definition. It would be a good idea to make the variables "private". This should possibly be your default whenever you are declaring variables.

    I.e: cell_1.setBrand(HTC);will not work
    Why doesn't it work? If you can't understand the compiler message copy and post the whole thing; I'm sure someone can explain what it's getting at.

    why is setting a variable public bad? I know it can be changed later on, but it can only be changed through the source code, not the console
    This is probably a question for Google (+ your classmates + your textbook + your teacher + ...) but I'll give my 2c. All variables, whatever their access modifiers, can only be changed as a result of things in the source code. It's the source code which specifies what the program does and changing the value of variables (for good or ill) is one of the things that the program does.

    What the access modifier "private" does is limit which pieces of source code can refer to a variable (and hence either get or set its value). The value of this lies in the fact that it allows pieces of source code (eg a class) to be "self contained". To give an example of what I mean by "self contained" imagine you have a class with some int variable foo and you really, really care that foo is never zero. Perhaps you intend using foo as the denominator of a fraction that controls a nuclear power plant. So you check, in the constructor (see above), that foo is given a non zero value when the PowerPlant instance is created. And you make sure that *every* method in the PowerPlant class that alters foo likewise makes sure that zero is never used as a value.

    You look through the PowerPlant class very, very closely and convince yourself that foo is never zero. But later (maybe much later) someone (maybe not even you) is working on some other class which contains code which creates an instance of PowerPlant. That's no problem because foo will never be zero. Except that you forgot and left the variable as "public"...

    The other person (maybe you because, over time, you forgot how important it was) writing some other code is perfectly free to write

    Java Code:
    PowerPlant myPP = new PowerPlant();
    // ...
    myPP.foo = 0;
    At that point very bad things happen.

    Lots of computer languages have some notion of "module" that allows complex problems to be broken down into smaller chuncks. The chunks are self contained in the sense that a close reading of them allows you to reach certain conclusions about their behaviour without having to consider to the rest of a more complex program. "private" is part of Java's implementation of this approach.
    Last edited by pbrockway2; 03-24-2015 at 07:41 AM.

  3. #3
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,991
    Rep Power
    8

    Default Re: Need advice on classes!

    Quote Originally Posted by Manddd View Post
    3) Why can I not set the string to whatever I want in my main class ( I.e: cell_1.setBrand(HTC);will not work)
    That is not a String. This is a String:
    Java Code:
    String HTC1 = "HTC";
    cell_3.setBrand(HTC1);
    or shorter:
    Java Code:
    cell_3.setBrand("HTC");
    Last edited by SurfMan; 03-24-2015 at 07:27 AM. Reason: Better example
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Need advice on classes!

    Quote Originally Posted by pbrockway2 View Post
    At that point very bad things happen.
    And those Very Bad Things will be hard to find.
    In the "setter" world, where attributes are made private, I could put a break point in the setFoo code and spot where my attribute is altered and trace back to the cause in the debugger.
    With a "myPP.foo = 0" call I would have to trawl through the code to find it.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Re: Need advice on classes!

    Quote Originally Posted by pbrockway2 View Post

    A constructor looks a lot like a method, but (1) It has the same name as the class, and (2) It does not have a return type, not even void.

    In your code you give some bogus values to brand, serialNumber, and price. Giving values to those variables is what the constructor should do. (That's the first point in the instructions). So keep the variables but remove the assignment of values to them and have a go at adding a constructor.



    What allows you to be able to set the values later is that each of them has an associated setter method: setBrand() etc. Those methods don't need the variables to be public in order to function because they are part of the class definition. It would be a good idea to make the variables "private". This should possibly be your default whenever you are declaring variables.
    Well after looking it up, my book just shows the constructor as initializing the parameters. However, it does it in different ways. Either by 1) setting a fix values to the parameters or 2) creating parameters in its parentheses. After creating the parameters, it then creates a method, which I thought was only used outside of a constructor as the constructor only initializes the parameters and nothing else.

    I'm asking this because in the code I showed up there, I was able to ( apparently) create the objects as cell_3 did create successfully as it showed is attributes ( anyway, it did in eclipse, so I think it created?).


    As for the private/ public. If you set your initial parameter as private, would it be possible to use the private variable in the set command?
    I.e: private String Brand = br

    .... public void setBrand(String br)
    {


    brand = br;
    }

    Would that allow me to set my own brand in my main method? as in

    cell_1.setBrand("YourCell"); ?

    Or did I get that completely wrong?

    SOrry for the lengthy questions, my teacher is not really helpful and reads straight from the book so I make do with the latter and online tutorials!

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Need advice on classes!

    Either by 1) setting a fix values to the parameters or 2) creating parameters in its parentheses.
    Method 2 is the one that meets the specifications you gave originally. "Upon the creation of a cell phone object, the object must immediately be initialized with valid values". The critical bit is "valid". If the constructor uses some random values (or if you assign them directly to the variables as you did on the code you posted) then those values won't be valid.

    I'm asking this because in the code I showed up there, I was able to ( apparently) create the objects as cell_3 did create successfully as it showed is attributes
    Here you used a setter method to set the attributes. That's fine, but it doesn't meet the requirements of the specification which include " the object must immediately be initialized with valid values". The most straightforward way of ensuring that the values are both valid and set immediately is to use a constructor which is passed those values. This setting of a valid initial state of an instance is what we have constructors for.

    As for the private/ public. If you set your initial parameter as private, would it be possible to use the private variable in the set command?
    I think I mentioned that: "Those methods [the setter methods] don't need the variables to be public in order to function because they are part of the class definition".

  7. #7
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Re: Need advice on classes!

    Then, for the object to be initialized, I would need to create a conductor like this so the 3 objects have different values ( correct me if I'm wrong):

    public Cellphone (String Brand, long serial, double price)
    {
    setCellphone(Brand, serial, price)
    }

    Assuming I initialized the parameter at the beginning <---- I mean intialized like "private String brand".

    And then by creating the method setPrice I can change the price later on of that object. Would that be because my constructor is not set with constant?

  8. #8
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Need advice on classes!

    The way you declare the constructor is correct. But you don't have a setCellphone() method, so I don't know what that's about. The most straightforward thing to do is to take the brand, serial, and price which were passed to the constructor and assign them to the attribute variables.

    I mean intialized like "private String brand"
    Small point, but that's a declaration of the attribute brand, not an initialisation.

    ---

    Bottom line: declare the attribute variables as private and assign to them in the constructor using the values that were passed. If the compiler complains, and you can't figure out what its messages mean, post them.

  9. #9
    Manddd is offline Member
    Join Date
    Mar 2015
    Posts
    23
    Rep Power
    0

    Default Re: Need advice on classes!

    Then would you be able to help me with this:

    I did everything asked of part one except for number 3 where it askes me to allow to view indicvidual details. \

    I wrote S.o.p(Cell_1.Price) or S.o.p(Cell_1.Pr) or S.o.p(Cell_1.pr)

    All of those tells me that either "Price is not accessible" or "Pr cannot be resolved or is not in a field" ( from the compiler)

    This is what my class looks like:

    Java Code:
     class Cellphone {
    	 
    	 private String brand;
    	 private long serialNumber;
    	 private double price;
    	 
    	 public Cellphone(String br, long sN, double Pr)
    	 {
    	    brand = br;
    	    serialNumber = sN;
    	    price = Pr;
    	    
    	    
    	 }
    public double getPrice()
    		{
    		     
    			return price;
    		}
    
    		public void setPrice(double pr)
    		{
    		    
    		     
    			price = pr;
    		}
    well atleast for the price!

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,717
    Rep Power
    17

    Default Re: Need advice on classes!

    All of those tells me that either "Price is not accessible" or "Pr cannot be resolved or is not in a field" ( from the compiler)
    price is private so to use its value in a System.out.println() statement you gave to get the value with getPrice().

Similar Threads

  1. not compiling classes to target/ classes
    By vandunen in forum Apache Maven
    Replies: 0
    Last Post: 11-12-2013, 04:55 PM
  2. Classes Help!!! two classes with object question.
    By stuckonjava in forum New To Java
    Replies: 16
    Last Post: 02-10-2012, 01:39 AM
  3. need for some advice
    By kasiopi in forum AWT / Swing
    Replies: 3
    Last Post: 01-26-2011, 12:36 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •