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
«1

Comments

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


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


  • Registered Users Posts: 7,081 ✭✭✭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,334 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 Posts: 7,081 ✭✭✭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,334 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 Posts: 7,081 ✭✭✭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 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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 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 Posts: 7,081 ✭✭✭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 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 Posts: 7,081 ✭✭✭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 Posts: 6,250 ✭✭✭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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭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 Posts: 7,081 ✭✭✭the whole year inn


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


  • Registered Users 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 Posts: 1,461 ✭✭✭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 Posts: 7,081 ✭✭✭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 Posts: 1,461 ✭✭✭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.


Advertisement