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
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java:Discount

«1

Comments

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


    if(bag >= 10 && < 20)
    if(bag >= 10 && bag < 20)


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


    croo wrote: »
    if(bag >= 10 && bag < 20)

    Yep thanks.So simple but if I wasnt told prob would never have got it.Ill know for again thanks!


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


    I've made the same mistake myself ,,, more than once over the years. But, as I said in stakeoverflow, if you read the syntax error message produced carefully you can see it did give some good clues. (ps. you can accept my stackoverflow answer if you wish ;))

    And learning to reading those compiler errors is a good skill to learn, so I would agree with your lecturer that, in these early stages of learning the language, a simple editor can be better than an ide.


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


    croo wrote: »
    I've made the same mistake myself ,,, more than once over the years. But, as I said in stakeoverflow, if you read the syntax error message produced carefully you can see it did give some good clues. (ps. you can accept my stackoverflow answer if you wish ;))

    And learning to reading those compiler errors is a good skill to learn, so I would agree with your lecturer that, in these early stages of learning the language, a simple editor can be better than an ide.

    Yea accpected that on stack overflow.I find it very hard to learn.Takes hours to learn simple things.The exam not open book so have to remember everything and something like that mess it up.
    Its a written exam aswell so if go wrong hard to know.


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


    Its a written exam
    All the more reason to use a simple editor rather than an IDE that will auto-complete the code as you type!
    Takes hours to learn simple things
    I'm afraid learning programming languages is just like that. Even when you know a few and need to learn another, you just need to practise lots and lots and suddenly you'll find that you are writing valid code! that;s how it is for me anyway. When following examples I always type the code rather than any copy & pasting just to practise writing it. Realistically though, in the real world, people are using IDE that, these days, write a lot of the code and even correct simple typos (much like your phone will).


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


    croo wrote: »
    All the more reason to use a simple editor rather than an IDE that will auto-complete the code as you type!

    I'm afraid learning programming languages is just like that. Even when you know a few and need to learn another, you just need to practise lots and lots and suddenly you'll find that you are writing valid code! that;s how it is for me anyway. When following examples I always type the code rather than any copy & pasting just to practise writing it. Realistically though, in the real world, people are using IDE that, these days, write a lot of the code and even correct simple typos (much like your phone will).
    Yeah its very slow but have about 5 weeks till the exam so have the time.I found stock over flow are harsh enough.Got 2 down votes Im just a beginner and get nailed for just asking a question.


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    Ask a lecturer for a grading scheme for a past paper. Written code exams arent that bad, you have a question with 5 marks, but you could get all those 5 marks for written code that wouldn't even compile.
    You might get a mark for using am IF statement, another mark for have two conditions, another mark for declaring a variable, another mark for setting the result to a variable. Most of the time you lose no marks for silly syntax mistakes.
    Your answer above could have got full marks even with leaving out the variable name from the if statement.


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


    Senna wrote: »
    Ask a lecturer for a grading scheme for a past paper. Written code exams arent that bad, you have a question with 5 marks, but you could get all those 5 marks for written code that wouldn't even compile.
    You might get a mark for using am IF statement, another mark for have two conditions, another mark for declaring a variable, another mark for setting the result to a variable. Most of the time you lose no marks for silly syntax mistakes.
    Your answer above could have got full marks even with leaving out the variable name from the if statement.

    Ya I might do that allright.Theres a theroy part which is worth over 40% on its own so can learn that so its not to bad.


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


    import java.util.Scanner;


    public class Lab5Part2 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //Declare the variables
    Scanner input;
    int numberToRead;
    int value;
    int count=0;
    int sum=0;
    float average;

    // Initialize the Scanner object
    input = new Scanner(System.in);

    // Prompt the user to enter the number of numbers that they wish to enter...
    System.out.println("Please enter the number of integers you wish to read");
    // Read the users input
    numberToRead = input.nextInt();

    //Repeat the loop a "numberToRead" times.....
    for(int i=0;i<numberToRead;i++)
    {
    // Prompt the user to enter a number
    System.out.println("Please enter your number");
    // Read the users input
    value = input.nextInt();

    if(value%2==(0|
    //Increment the count of numbers divisible by 2
    count++
    sum+=value;
    }
    }

    // If the numbers were divisible by 2 & So to avoid dividing by zero
    if(count==0)
    System.out.println("no odd numbers");
    else
    {
    float sum;
    float count;
    // Calculate the average and cast it to a float...
    Object average = (float)sum/count;
    System.out.println("Average :"+average);
    }

    // Close the input stream
    input.close();
    }

    }


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


    Im trying to get a programme where it ask for an interger and to only use prime numbers and get the sum and average.
    I have ran into a few errors but what do you think of the code and any help with the errors?


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    You have a lot going on in that code, if I was you, I'd first work out how to find if a number was prime(sure you don't mean even?). Then work out how to find average and sum.
    Best way to learn is to write 3 different programs (methods), each working out a different calculation. Once you have all 3 working, combine them into one program where you only enter the numbers once
    Break all problems down to as small as possible, much easier to get an answer.


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


    Senna wrote: »
    You have a lot going on in that code, if I was you, I'd first work out how to find if a number was prime(sure you don't mean even?). Then work out how to find average and sum.
    Best way to learn is to write 3 different programs (methods), each working out a different calculation. Once you have all 3 working, combine them into one program where you only enter the numbers once
    Break all problems down to as small as possible, much easier to get an answer.

    import java.util.Scanner;


    public class Lab5Part2 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //Declare the variables
    Scanner input;
    int numberToRead;
    int count=0;


    // Initialize the Scanner object
    input = new Scanner(System.in);

    // Prompt the user to enter the number of numbers that they wish to enter...
    System.out.println("Please enter the number of integers you wish to read");
    // Read the users input
    numberToRead = input.nextInt();

    //Repeat the loop a "numberToRead" times.....
    for(int i=0;i<numberToRead;i++)
    {
    // Prompt the user to enter a number
    System.out.println("Please enter your number");
    // Read the users input


    // Close the input stream
    input.close();
    }

    }
    }


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


    When i do this it runs put prints out please enter number what ever number I enter first?I cant enter the numbers onlynthe number of intergers I want to enter.


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


    import java.util.Scanner;


    public class Lab5Part2 {

    public static void main(String[] args) {
    // TODO Auto-generated method stub

    //Declare the variables
    Scanner input;
    int numberToRead;
    int count=0;
    int value;

    // Initialize the Scanner object
    input = new Scanner(System.in);

    // Prompt the user to enter the number of numbers that they wish to enter...
    System.out.println("Please enter the number of integers you wish to read");
    // Read the users input
    numberToRead = input.nextInt();
    count++;
    //Repeat the loop a "numberToRead" times.....
    for(int i=0;i<numberToRead;i++)
    {
    // Prompt the user to enter a number
    System.out.println("Please enter your number");
    // Read the users input
    value = input.nextInt();


    }

    // Close the input stream
    input.close();
    }

    }


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    When i do this it runs put prints out please enter number what ever number I enter first?I cant enter the numbers onlynthe number of intergers I want to enter.

    Read this part again, you need something between the two comments. Think about what you want to happen here.
    Also delete the input.close


    // Read the users input


    // Close the input stream
    input.close();


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


    Senna wrote: »
    Read this part again, you need something between the two comments. Think about what you want to happen here.
    Also delete the input.close


    // Read the users input


    // Close the input stream
    input.close();

    I got the enter the number of intergers the user wants and to enter the numbers.

    Im stuck now on using only prime number so was thinking going along the lines if the value can be /2 then dont use which i think is if (value % 2==1)
    And use an else statement am I along the right lines?


  • Registered Users, Registered Users 2 Posts: 8,800 ✭✭✭Senna


    I got the enter the number of intergers the user wants and to enter the numbers.

    Im stuck now on using only prime number so was thinking going along the lines if the value can be /2 then dont use which i think is if (value % 2==1)
    And use an else statement am I along the right lines?

    In all honesty, if I had that question in college, I'd be googling how to find out what a prime number is, I'm sure you are told not to Google code but theory's are find to Google.

    For the record, I wouldn't remember code on how to find prime numbers, if I had to do it tomorrow, I'd be straight on Google first, but you will need an if statement in a for loop, testing each number.


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


    Senna wrote: »
    In all honesty, if I had that question in college, I'd be googling how to find out what a prime number is, I'm sure you are told not to Google code but theory's are find to Google.

    For the record, I wouldn't remember code on how to find prime numbers, if I had to do it tomorrow, I'd be straight on Google first, but you will need an if statement in a for loop, testing each number.

    The exam is closed book so have to remember the code.
    Dont know how im supposed to know the code with out googling.I can have an idea how to go about and learn the code and then try and implement it to the questions.

    Yea i have an if statement just the if statement i have is wrong or doesnt work


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭Buford T Justice


    The exam is closed book so have to remember the code.
    Dont know how im supposed to know the code with out googling.I can have an idea how to go about and learn the code and then try and implement it to the questions.

    Yea i have an if statement just the if statement i have is wrong or doesnt work

    If you're trying to learn how to do the assignment, what is preventing you from using google as a study aid?

    Also, what have you tried in the if statement. and use code tags when adding code


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


    If you're trying to learn how to do the assignment, what is preventing you from using google as a study aid?

    Also, what have you tried in the if statement. and use code tags when adding code

    I am using google but prefer talking about the specific problem and learn.

    I moved onto a new problem

    package RectangleDemo;

    class Rectangle {
    private double length=10;
    private double breadth=20;
    }

    // This class declares an object of type Rectangle.
    public class RectangleDemo (double length,breadth;)

    { double length=10;
    double breadth=20;}

    public static void main(String args[]) {
    Rectangle myrect1 = new Rectangle();
    double area1,prim1;


    // Compute Area of Rectangle
    area1 = myrect1.length * myrect1.breadth ;
    System.out.println("Area of Rectange 2 : " + area1);



    // Compute Primeter of Rectangle
    prim1 = (myrect1.length*2) * (myrect1.breadth*2) ;
    System.out.println("Perimeter of Rectange 2 : " + prim1);

    }
    }


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


    d) Create the Class Rectangle for the object class definition below.

    Rectangle
    private:
    length: double
    width: double
    public:
    Rectangle(double length, double width)
    void setLength(double length)
    void setWidth(double width)
    double area()
    double perimeter()
    void printRectangle()


    Information:
    The output of the printRectangle () method should be:
    Length: length
    Width: width
    E.g.
    Length: 2.0, Width: 3.0, The area method() should return the area of the Rectangle i.e. length * width; The perimeter() method should return the perimeter of the Rectangle i.e.
    (length * 2) +( width * 2);


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


    d) Create the Class Rectangle for the object class definition below.

    Rectangle
    private:
    length: double
    width: double
    public:
    Rectangle(double length, double width)
    void setLength(double length)
    void setWidth(double width)
    double area()
    double perimeter()
    void printRectangle()


    Information:
    The output of the printRectangle () method should be:
    Length: length
    Width: width
    E.g.
    Length: 2.0, Width: 3.0, The area method() should return the area of the Rectangle i.e. length * width; The perimeter() method should return the perimeter of the Rectangle i.e.
    (length * 2) +( width * 2);

    This pretty much gives you the answer. Take the start of the description and that forms your rectangle class:
    package rectangledemo;
    
    public class Rectangle {
    	// Private variables
    	private double length;
    	private double width;
    	
    	// Constructor - set the values
    	Rectangle(double length, double width) {
    		setLength(length);
    		setWidth(width);
    	}
    	
    	// Set the length
    	public void setLength(double length) {
    		this.length = length;
    	}
    	
    	// Set the width
    	public void setWidth(double width) {
    		this.width = width;
    	}
    	
    	// Calculate the area
    	public double area() {
    		return length * width;
    	}
    	
    	// Calculate the perimeter
    	public double perimeter() {
    		return (length * 2) + (width * 2);
    	}
    	
    	// Display the rectangle information
    	public void printRectangle() {
    		System.out.println("Length:" + length);
    		System.out.println("Width:" + width);
    	}
    }
    


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


    bpmurray wrote: »
    This pretty much gives you the answer. Take the start of the description and that forms your rectangle class:
    package rectangledemo;
    
    public class Rectangle {
    	// Private variables
    	private double length;
    	private double width;
    	
    	// Constructor - set the values
    	Rectangle(double length, double width) {
    		setLength(length);
    		setWidth(width);
    	}
    	
    	// Set the length
    	public void setLength(double length) {
    		this.length = length;
    	}
    	
    	// Set the width
    	public void setWidth(double width) {
    		this.width = width;
    	}
    	
    	// Calculate the area
    	public double area() {
    		return length * width;
    	}
    	
    	// Calculate the perimeter
    	public double perimeter() {
    		return (length * 2) + (width * 2);
    	}
    	
    	// Display the rectangle information
    	public void printRectangle() {
    		System.out.println("Length:" + length);
    		System.out.println("Width:" + width);
    	}
    }
    

    And then to make the run i have to make a test class,set the price and it works then?


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


    Also thanks for everyone helping me!!
    Count down 3 weeks to the exam and am learning a little bit everyday.


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


    package Product;

    public class Product {
    // Private variables
    private int id;
    private float price;
    private String name;

    public Product (int i,String s,float p)
    {id=i;
    name=s;
    price=p;
    }
    public int getId()
    {return id;
    }
    public void setId (String newId) {
    }

    public String getName()
    {return name;
    }
    public void setName (String newName) {
    }


    public float getPrice()
    {return price;
    }
    public void setprice (String newPrice) {
    }


    // Display the rectangle information
    public void printProduct() {
    System.out.println("Priceprice");
    System.out.println("Namename");
    System.out.println("Idid")
    }
    }


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


    This is the question,how does the code look?Am I along the right lines?


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


    This is the question,how does the code look?Am I along the right lines?

    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.


  • Registered Users, Registered Users 2 Posts: 1,465 ✭✭✭Anesthetize


    One thing to point out - 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.


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


    One thing to point out - 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.

    Yes ok,just the question said to use the single letters.I should use comments.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,465 ✭✭✭Anesthetize


    Yes ok,just the question said to use the single letters.I should use comments.
    I don't even understand why they do that. They should be encouraging students to write understandable code from an early stage. You shouldn't even have to use comments to explain what a piece of code does.


  • Registered Users, Registered Users 2 Posts: 7,123 ✭✭✭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, Registered Users 2 Posts: 7,123 ✭✭✭the whole year inn


    This is the question.


  • Registered Users, Registered Users 2 Posts: 6,262 ✭✭✭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, Registered Users 2 Posts: 7,123 ✭✭✭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, Registered Users 2 Posts: 6,262 ✭✭✭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, Registered Users 2 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, Registered Users 2 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, Registered Users 2 Posts: 7,123 ✭✭✭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, Registered Users 2 Posts: 7,123 ✭✭✭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,336 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, Registered Users 2 Posts: 7,123 ✭✭✭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,336 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, Registered Users 2 Posts: 7,123 ✭✭✭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, Registered Users 2 Posts: 7,123 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 788 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 7,123 ✭✭✭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, Registered Users 2 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, Registered Users 2 Posts: 7,123 ✭✭✭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
Advertisement