Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Java:Discount

Options
2»

Comments

  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    punk_one82 wrote: »
    Code looks okay. You're along the right lines, but there are a few mistakes. Try compiling that class and see what the compiler complains about. Specifically think about the logic and types in your set methods, as well as what's going on in your printProduct method.

    As an aside - I'd try work on your code formatting. It's pretty hard to read at the minute.

    I fixed the println,the closed it off and add a semi colon.Theres no errors in eclipse and when I run it terminates.

    I also have one problem with this question.
    I understand that its tondo with inheritance mammal extends dog but stuck on it.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    This is the question.


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    I fixed the println,the closed it off and add a semi colon.Theres no errors in eclipse and when I run it terminates.

    I also have one problem with this question.
    I understand that its tondo with inheritance mammal extends dog but stuck on it.

    What are you asking specifically?


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    What are you asking specifically?

    Any examples i find online just show a snippet of the code and I dont know how to answer the question.


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    Any examples i find online just show a snippet of the code and I dont know how to answer the question.

    So you want us to do your homework for you, or is there an aspect of the question you would like help with?

    Have you read something like this?


  • Advertisement
  • Registered Users Posts: 1,275 ✭✭✭bpmurray


    One other thing - when you post your code, surround it with a [ CODE] and a [ /CODE] (there shouldn't be spaces after the "[") to make it legible. What are you using to develop? Do you have Eclipse or something similar? That will format your code for you - formatting, meaningful variable names and comments are essential to make your code readable.

    Edit: your formatting seems to indicate that you're afraid of whitespace. When talking to 8-year-olds at CoderDojo, this is something I have to repeat quite often but they get it fairly quickly. I suggest you hit <ENTER> more often and include plenty of spaces. For example you have:
    public Product (int i,String s,float p)
    {id=i;
    name=s;
    price=p;
    }

    While the normal formatting (using [ CODE]) is:
    public Product (int i, String s, float p)
    {
        id    = i;
        name  = s;
        price = p;
    }
    
    Also, in this case, I'd emphasise that the variables are class variables by using this:
    public Product (int i, String s, float p)
    {
        this.id    = i;
        this.name  = s;
        this.price = p;
    }
    


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    I fixed the println,the closed it off and add a semi colon.Theres no errors in eclipse and when I run it terminates.

    I also have one problem with this question.
    I understand that its tondo with inheritance mammal extends dog but stuck on it.

    There may be no errors after you fixed your print method, but look at your set methods. What are they doing? Are the parameters you're passing in to those methods of the right type? Are they setting the values on any class variables?

    Your set methods are currently doing nothing at all, hence there being no errors. Make your set methods set something and then figure out what's wrong with the parameters.

    Edit: On the inheritance question - "mammal extends dog". Think about it logically. Does a mammal actually extend dog? A dog is a type of mammal -> A labrador is a type of dog. The arrows in the question give the hierarchy. Mammal -> Dog -> Labrador.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    punk_one82 wrote: »
    There may be no errors after you fixed your print method, but look at your set methods. What are they doing? Are the parameters you're passing in to those methods of the right type? Are they setting the values on any class variables?

    Your set methods are currently doing nothing at all, hence there being no errors. Make your set methods set something and then figure out what's wrong with the parameters.

    Edit: On the inheritance question - "mammal extends dog". Think about it logically. Does a mammal actually extend dog? A dog is a type of mammal -> A labrador is a type of dog. The arrows in the question give the hierarchy. Mammal -> Dog -> Labrador.
    package Product;
    
    public class Product {
    // Private variables
    private int id;
    private float price;
    private String name;
    
    public Product ( int i,String s,float p)
    {this.id=i;
    this.name=s;
    this.price=p;
    }
    public int getId()
    {return id;
    }
    public void setId (int newId) {
    	{
    id=newId;}
    }
    
    public String getName()
    {return name;
    }
    public void setName (String newName) {
    name=newName;
    }
    
    
    public float getPrice()
    {return price;
    }
    public void setprice (Float newPrice) {
    
    price=newPrice;
    }
    
    
    // Display the rectangle information
    public void printProduct() {
    System.out.println("Priceprice");
    System.out.println("Namename");
    System.out.println("Idid");
    }
    		}
    ]
    
    I had another stab at it,how does it look now,if im honest I think its right.Im getting afew grinds tomorrow so hopefully that helps.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    So you want us to do your homework for you, or is there an aspect of the question you would like help with?

    Have you read something like this?

    Thanks I learning this now.


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    how does it look now,if im honest I think its right.
    Do you get the results you expect when you run it?
    I don't image so!

    To re-iterate a couple of points others have already said.
    never use single letters for variable or parameter names. Use something descriptive. Other people need to be able to understand what the code is doing just by reading the code.
    And you wrote...
    public Product ( int i,String s,float p) {
         this.id=i;
         this.name=s;
         this.price=p;
    }
    

    But, at least you did follow bpmurray's advice of using this
    bpmurray wrote: »
    in this case, I'd emphasise that the variables are class variables by using this:
    but only for the one method he provided and example for but not in any other method.

    It will still compile & run; it's just not considered best practise. On a small exercise like this it will make little difference but it's a bad habit to get into so why not just do it right to start with and not develop any of those bad habits!

    So standard practice would be to use descriptive variable names and when setting those use the this keyword.
    public Product ( int id, String name, float price) {
         this.id = id;
         this.name = name;
         this.price = price;
    }
    

    And that practise should be applied applied to all methods so, taking just one, as an example.
    public void setId (int id) {
         this.id = id;
    }
    
    Would be following best practise.


  • Advertisement
  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    croo wrote: »
    Do you get the results you expect when you run it?
    I don't image so!

    To re-iterate a couple of points others have already said.

    And you wrote...
    public Product ( int i,String s,float p) {
         this.id=i;
         this.name=s;
         this.price=p;
    }
    

    But, at least you did follow bpmurray's advice of using this
    but only for the one method he provided and example for but not in any other method.

    It will still compile & run; it's just not considered best practise. On a small exercise like this it will make little difference but it's a bad habit to get into so why not just do it right to start with and not develop any of those bad habits!

    So standard practice would be to use descriptive variable names and when setting those use the this keyword.
    public Product ( int id, String name, float price) {
         this.id = id;
         this.name = name;
         this.price = price;
    }
    

    And that practise should be applied applied to all methods so, taking just one, as an example.
    public void setId (int id) {
         this.id = id;
    }
    
    Would be following best practise.

    Well the questions stats to use i,s etc,I am just following the what the question is looking for.
    I will add this to the other methods.
    For the code to work whats wrong with it?


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    Well the questions stats to use i,s etc,
    Fair enough.
    For the code to work whats wrong with it?
    Well does it compile?
    Does it provide you the result you were expecting?


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    croo wrote: »
    Fair enough.

    Well does it compile?
    Does it provide you the result you were expecting?

    Well for the question that was asked from the exam question,I think it does.I will get the question again and see what you think.

    Thats the question.
    https://us.v-cdn.net/6034073/uploads/attachments/10189/393253.jpg


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    So you want us to do your homework for you, or is there an aspect of the question you would like help with?

    Have you read something like this?
    class mammal {
    	String gender;
    	
    	public mammal(string g){
    		gender = g;
    	}
    }
    
    ==========================================================
    
    class dog extends mammal{
    
    	String breed;
    	
    	public dog(string g, string b){
    		super(g);
    		breed = b;
    	}
    	
    }
    
    ==============================================================
    class labrador extends dog {
    	
    	String eyecolor;
    	
    	public labrador(String g, String b, String ec){
    		super.(g,b)
    		eyecolor = ec;
    	}
    	
    }
    

    Thats the answer I have for that question


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    class mammal {
    	String gender;
    	
    	public mammal(string g){
    		gender = g;
    	}
    }
    
    ==========================================================
    
    class dog extends mammal{
    
    	String breed;
    	
    	public dog(string g, string b){
    		super(g);
    		breed = b;
    	}
    	
    }
    
    ==============================================================
    class labrador extends dog {
    	
    	String eyecolor;
    	
    	public labrador(String g, String b, String ec){
    		super.(g,b)
    		eyecolor = ec;
    	}
    	
    }
    

    Thats the answer I have for that question

    You're getting there. Still don't think it will compile, but if you try compile it the problems should be glaringly obvious.

    You coding style has improved drastically, but it's still best practice to use the "this" keywork when referencing class level variables eg. this.eyecolour = ec;

    I'd probably use an abstract base class also, but not sure that's required for the question


  • Registered Users Posts: 773 ✭✭✭pillphil


    class mammal {
    	String gender;
    	
    	public mammal(string g){ // line 1
    		gender = g;
    	}
    }
    
    ==========================================================
    
    class dog extends mammal{
    
    	String breed;
    	
    	public dog(string g, string b){ // line 2
    		super(g);
    		breed = b;
    	}
    	
    }
    
    ==============================================================
    class labrador extends dog {
    	
    	String eyecolor;
    	
    	public labrador(String g, String b, String ec){
    		[B]super.(g,b)[/B]//line 3
    		eyecolor = ec;
    	}
    	
    }
    

    Thats the answer I have for that question

    Program fails to compile due to errors in lines 1, 2 and 3?

    Also, should classes begin with a capital letter?

    Are you actually trying to run the code? That will show you where you are going wrong pretty quickly.


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    As above, try compile the code when you're writing it. If you're practicing for a handwritten test write it in notepad and try compile it when you think you're done. You'll start spotting your common errors and can get the correct way of doing it in your head.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    punk_one82 wrote: »
    As above, try compile the code when you're writing it. If you're practicing for a handwritten test write it in notepad and try compile it when you think you're done. You'll start spotting your common errors and can get the correct way of doing it in your head.

    Thanks everyone.I am getting a little bit more confident.In reguards to the to the product class I have done above,can someone tell me whats exactly wrong with it.
    I known I have to add this. In parts but other than that what else is wrong?I thought it was right.
    I add it to eclipse and check but in the product class I dont get an error so not sure why some posters said it was wrong or why its wrong.Can anyone help with that?


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    class [B]M[/B]ammal {
    	String gender;
    	
    	public [B]M[/B]ammal([B]S[/B]tring g){
    		[B]this.[/B]gender = g;
    	}
    }
    
    class [B]D[/B]og extends [B]M[/B]ammal{
    
    	String breed;
    	
    	public [B]D[/B]og([B]S[/B]tring g, [B]S[/B]tring b){
    		super(g);
    		[B]this.[/B]breed = b;
    	}
    	
    }
    
    class [B]L[/B]abrador extends [B]D[/B]og {
    	
    	String eyecolor;
    	
    	public [B]L[/B]abrador(String g, String b, String ec){
    		[B]super(g,b);[/B]
    		[B]this.[/B]eyecolor = ec;
    	}
    	
    }
    

    Compare this to your code.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    punk_one82 wrote: »
    class [B]M[/B]ammal {
    	String gender;
    	
    	public [B]M[/B]ammal([B]S[/B]tring g){
    		[B]this.[/B]gender = g;
    	}
    }
    
    class [B]D[/B]og extends [B]M[/B]ammal{
    
    	String breed;
    	
    	public [B]D[/B]og([B]S[/B]tring g, [B]S[/B]tring b){
    		super(g);
    		[B]this.[/B]breed = b;
    	}
    	
    }
    
    class [B]L[/B]abrador extends [B]D[/B]og {
    	
    	String eyecolor;
    	
    	public [B]L[/B]abrador(String g, String b, String ec){
    		[B]super(g,b);[/B]
    		[B]this.[/B]eyecolor = ec;
    	}
    	
    }
    

    Compare this to your code.

    Ok I will thanks


  • Advertisement
  • Registered Users Posts: 773 ✭✭✭pillphil


    In relation to the product class, I don't want to just give you the answer, but if I create a new product object
    Product p = new Product(123, "Milk", 1.20f);
    

    and call the printProduct method
    p.printProduct();
    

    I expect to see this as an output (as per the question)

    394155.PNG
    ID: 123
    Name: Milk
    Price: &#8364;1.20
    

    However, what actually is output is
    Priceprice
    Namename
    Idid
    


    I don't mean to be an arse, but if you have to ask why it's wrong, it means you either didn't compile and run the code or you don't understand the question.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    pillphil wrote: »
    In relation to the product class, I don't want to just give you the answer, but if I create a new product object
    Product p = new Product(123, "Milk", 1.20f);
    

    and call the printProduct method
    p.printProduct();
    

    I expect to see this as an output (as per the question)

    394155.PNG
    ID: 123
    Name: Milk
    Price: &#8364;1.20
    

    However, what actually is output is
    Priceprice
    Namename
    Idid
    


    I don't mean to be an arse, but if you have to ask why it's wrong, it means you either didn't compile and run the code or you don't understand the question.
    Your not an arse,helping alot actuially!
    I emailed the lecture about this and asked do I have to do a test class to print out the info,as I would understand how to do that?He never got back to me.
    Or can I do it all from this and not create a new test class?


  • Registered Users Posts: 6,250 ✭✭✭Buford T Justice


    Your not an arse,helping alot actuially!
    I emailed the lecture about this and asked do I have to do a test class to print out the info,as I would understand how to do that?He never got back to me.
    Or can I do it all from this and not create a new test class?

    Just create another class with a main method that creates the object and printproduct method


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    Just create another class with a main method that creates the object and printproduct method

    Thanks,thats what I was getting at,I thought the question said to create a class which I thought was one,in other questions it asks for the other class to be created so that where I misunderstood.
    thanks !


  • Moderators, Technology & Internet Moderators Posts: 1,334 Mod ✭✭✭✭croo


    I emailed the lecture about this and asked do I have to do a test class to print out the info
    pillphil didn't mean that you didn't have code to test print the product info but that the method you have
    // Display the rectangle information
    public void printProduct() {
    System.out.println("Priceprice");
    System.out.println("Namename");
    System.out.println("Idid");
    }
    
    does not actually print the info!

    PS. even if the method was printing the product info the layout you have does not match that requested. A minor issue perhaps while learning to code but for an exam question it's these minor issues that might mean the difference between a correct answer and a perfect answer!


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    croo wrote: »
    pillphil didn't mean that you didn't have code to test print the product info but that the method you have
    // Display the rectangle information
    public void printProduct() {
    System.out.println("Priceprice");
    System.out.println("Namename");
    System.out.println("Idid");
    }
    
    does not actually print the info!

    PS. even if the method was printing the product info the layout you have does not match that requested. A minor issue perhaps while learning to code but for an exam question it's these minor issues that might mean the difference between a correct answer and a perfect answer!

    Ok I will try and get this right,it has came up a few times in the exam papers so would like to get it right.


  • Registered Users Posts: 773 ✭✭✭pillphil


    croo wrote: »
    pillphil didn't mean that you didn't have code to test print the product info but that the method you have
    // Display the rectangle information
    public void printProduct() {
    System.out.println("Priceprice");
    System.out.println("Namename");
    System.out.println("Idid");
    }
    
    does not actually print the info!

    PS. even if the method was printing the product info the layout you have does not match that requested. A minor issue perhaps while learning to code but for an exam question it's these minor issues that might mean the difference between a correct answer and a perfect answer!

    Annoyingly, it gives two different sample layouts.

    @the whole year in
    The exam question doesn't require you to have a test class to execute the code you've written, however you should write one if only to see what your code is actually doing.


  • Registered Users Posts: 7,085 ✭✭✭the whole year inn


    Had the exam today,thought it was hard but I passed I think(hopefully) thanks everyone who helped here.


  • Registered Users Posts: 1,148 ✭✭✭punk_one82


    Had the exam today,thought it was hard but I passed I think(hopefully) thanks everyone who helped here.

    Congratulations. Keep plugging away. Some things in programming seem difficult or bizarre at first, but then they click and become second nature.


  • Advertisement
Advertisement