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 Programming

Options
  • 28-11-2012 9:29pm
    #1
    Registered Users Posts: 34


    how in java do you ask user how many values they want to enter and get the smallest number of the list of numbers?

    HELP BADLY NEEDED!!!!!! This is an exam question and i cant seem to get it! here is code that ive done but its based on entering three numbers. Could someone explain how it do this! Please I am really struggling.

    import java.util.Scanner;

    public class Average
    {
    public static void main(String [] args)
    {
    double num1, num2, num3, sum, average;
    Scanner input=new Scanner(System.in);

    System.out.println ("Enter first number: ");
    num1=input.nextInt();

    System.out.println ("Enter second number: ");
    num2=input.nextInt();

    System.out.println ("Enter third number: ");
    num3=input.nextInt();

    sum=sum(num1, num2, num3);

    average=sum/3;

    System.out.println("The average of " + num1+ " , " +num2+ " , " +num3)
    }
    public static double sum (double num1, double num2, double num3)
    {
    return num1+num2+num3;
    }

    public static double average (double num1, double num2, double num3)
    {
    return sum(num1,num2,num3)/3;
    }
    }
    }


Comments

  • Closed Accounts Posts: 2,117 ✭✭✭Defiler Of The Coffin


    Have you covered for loops yet? Do you know what an array list is?


  • Registered Users Posts: 34 mise1992


    Have you covered for loops yet? Do you know what an array list is?

    no i dnt know what either of them are sorry. would u mind informing me of them


  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/




  • Registered Users Posts: 8,105 ✭✭✭batistuta9


    mise1992 wrote: »
    how in java do you ask user how many values they want to enter and get the smallest number of the list of numbers?

    HELP BADLY NEEDED!!!!!! This is an exam question and i cant seem to get it! here is code that ive done but its based on entering three numbers. Could someone explain how it do this! Please I am really struggling.

    If you're revising for exams & haven't covered arrays, loops or whatever it's unlikely they'll come up on the exam. But you may well start doing them shortly

    The code below will do what you've asked in the first line (you've the sum & average in your code but not in the question, do you need them too?), there's no error handling in it though

    + the video linked in the other post will give you an explanation of arrays & loops and save me the bother :pac:
    import java.util.*;
    
    public class Numbers
    {
      public static void main(String[] args)
      {
        Scanner kIn = new Scanner(System.in);
        int numbers=0;
        int smallest=0;
    	
        System.out.print("How many numbers do you want to enter: ");
        numbers=kIn.nextInt();
        
        int[] array = new int[numbers];
    	
        for(int i=0; i<numbers; i++)
        {
          System.out.print("Enter number "+(i+1)+": ");
          array[i]=kIn.nextInt(); 
        }
    	 
        smallest=array[0];
        for(int i=0; i<numbers; i++)
        {
          if(array[i]<smallest)
            smallest=array[i];
        }
    	 
        System.out.print("The smallest number you entered is: "+smallest);
      }
    }
    


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    Doing people's college assignments for them in this manner, just harms everyone. It harms the original poster because they dont learn anything, and it hurts professional developers as one day we may need to work with these people.

    OP, for your sake lets hope your prof doesn't browse this forum.


  • Advertisement
  • Registered Users Posts: 34 mise1992


    ChRoMe wrote: »
    Doing people's college assignments for them in this manner, just harms everyone. It harms the original poster because they dont learn anything, and it hurts professional developers as one day we may need to work with these people.

    OP, for your sake lets hope your prof doesn't browse this forum.

    first of all this isnt an assignment! secondly u know nothing about how i learn! for all u know i could b using this example for a range of different programs. and if my lecture does happen to come across all the better they can see that we are puttin in the effort in our assignments and nt just givin up at the first hurdle! its and i say a huge thanks to the previous posters as they have help me loads!


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


    mise1992 wrote: »
    first of all this isnt an assignment! secondly u know nothing about how i learn! for all u know i could b using this example for a range of different programs. and if my lecture does happen to come across all the better they can see that we are puttin in the effort in our assignments and nt just givin up at the first hurdle! its and i say a huge thanks to the previous posters as they have help me loads!

    Good grief! I hope you're not looking for a job as a writer.


  • Closed Accounts Posts: 5,857 ✭✭✭professore


    If you haven't done loops yet they haven't helped you at all - as your lecturer won't expect you to answer that way.


  • Closed Accounts Posts: 5,857 ✭✭✭professore


    public static void main(String [] args)
    {
    double num1, num2, num3, sum, average, smallest;
    Scanner input=new Scanner(System.in);

    System.out.println ("Enter first number: ");
    num1=input.nextInt();

    smallest = num1;

    System.out.println ("Enter second number: ");
    num2=input.nextInt();

    if(num2 < smallest)
    smallest = num2;

    System.out.println ("Enter third number: ");
    num3=input.nextInt();

    if(num3 < smallest)
    smallest = num3;

    System.out.println("The smallest number is " + smallest );
    }

    No arrays or loops needed.


  • Closed Accounts Posts: 595 ✭✭✭tony81


    Op, given your code you're nowhere near to getting this problem done.

    Figure out the logic of what you're trying to program and bin that old code as (bar the scanner line) it's completely irrelevant to what you're trying to do.

    Your code asks for three numbers, send them to a method to total them, then divides by three.
    The code you need should ask the user how many numbers they wish to enter, each time a number is entered figure out if it is the lowest entered so far and, if so, store it as the minimum, then output the minimum.

    Sorry, but your post is typical of a student who has little interest in the subject.


  • Advertisement
  • Closed Accounts Posts: 595 ✭✭✭tony81


    professore wrote: »
    public static void main(String [] args)
    {
    double num1, num2, num3, sum, average, smallest;
    Scanner input=new Scanner(System.in);

    System.out.println ("Enter first number: ");
    num1=input.nextInt();

    smallest = num1;

    System.out.println ("Enter second number: ");
    num2=input.nextInt();

    if(num2 < smallest)
    smallest = num2;

    System.out.println ("Enter third number: ");
    num3=input.nextInt();

    if(num3 < smallest)
    smallest = num3;

    System.out.println("The smallest number is " + smallest );
    }

    No arrays or loops needed.

    First, wrong answer.

    The op's problem stated "ask user how many values they want to enter and get the smallest number of the list of numbers"

    Also, I fail to see how giving the op an answer is at all helpful.

    I'm currently doing a very basic course in computers and we're plagued by people who have no idea, do irrelevant, non-working code, and get someone else in the class to "fix it up" for them.

    As a result there's rumours our C.A results are being withheld because they suspect plagiarism. They're going to wait until we do an open-book, class test to find out the people who "got help" (i.e. cheated in their assignments).


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    mise1992 wrote: »
    first of all this isnt an assignment! secondly u know nothing about how i learn! for all u know i could b using this example for a range of different programs. and if my lecture does happen to come across all the better they can see that we are puttin in the effort in our assignments and nt just givin up at the first hurdle! its and i say a huge thanks to the previous posters as they have help me loads!

    No, you will just be punished for plagiarism.


  • Registered Users Posts: 8,105 ✭✭✭batistuta9


    ChRoMe wrote: »
    Doing people's college assignments for them in this manner, just harms everyone. It harms the original poster because they dont learn anything, and it hurts professional developers as one day we may need to work with these people.

    OP, for your sake lets hope your prof doesn't browse this forum.

    From reading the OP it looks as if mise1992 is looking at old exam papers and trying to do a question from it, now that could be wrong of course and it's actually an assignment they've been given but going by their first line
    how in java do you ask user how many values they want to enter and get the smallest number of the list of numbers?

    i doubt it is it, since they said they don't know either arrays or loops. But all thats giving mise1992 the benefit of the doubt, even with, for some reason their code has methods for getting the average & sum.

    I can see your point but i'm going to disagree with it

    if mise1992 doesn't understand arrays & loops they're going to have to look into them, from either the videos linked or the code i posted, which doesn't explain anything to him/her about what's going on

    if they want to understand it they will, they'll have to figure out what's going on in the code to make it work & why it's the way it is - surely understanding how it works will make them a better programmer, no?

    + In colleges a lot of lectures don't even provide solutions to the labs they do for the students, would you really think that's helpful to all students, particularly ones who're struggling? It's often easier to see how you should go about doing something or even what's the best way to go about doing it.


  • Registered Users Posts: 2,021 ✭✭✭ChRoMe


    batistuta9 wrote: »
    From reading the OP it looks as if mise1992 is looking at old exam papers and trying to do a question from it, now that could be wrong of course and it's actually an assignment they've been given but going by their first line

    Read the first and fourth sentences of the post 7 :)

    The point is that cutting and pasting code from a forum doesn't enhance your knowledge in any way. They have gained precisely nothing except for the expected output which is useless to them.

    I'd very happily spend time here (as I have on other forums) walking someone new through a problem. They get to get that "aha" moment in their own mind for the concepts to be understood, and that is achieved by writing the code yourself and running it successfully for the first time.


  • Registered Users Posts: 8,105 ✭✭✭batistuta9


    ChRoMe wrote: »
    Read the first and fourth sentences of the post 7 :)

    The point is that cutting and pasting code from a forum doesn't enhance your knowledge in any way. They have gained precisely nothing except for the expected output which is useless to them.

    I'd very happily spend time here (as I have on other forums) walking someone new through a problem. They get to get that "aha" moment in their own mind for the concepts to be understood, and that is achieved by writing the code yourself and running it successfully for the first time.

    yeah, maybe it was an assignment :pac:

    but there's no reason why they can't go on and learn from it - reverse engineer the code, if they're interested in learning to program they'll do it, if not they'll be tripped up by it sooner or later


  • Registered Users Posts: 34 mise1992


    While i have ye all here, on a different topic completely, class hierarchy.

    i have a parent class Animals with arrtibutes name, birth and speciecs. I have too child classes then Mammels and Reptiles with the same attributes as Animal along with NumLegs and whether they are a carnivore. However along ALL them Mammels have weight and height attributes and reptiles has an additional attribute of length. when i am writing me classes would i be ok to say:

    public abstract class Reptiles extend Mammels

    or do i need to say:

    public class Reptiles extends Animals


    Thanks everyone for the pervious answers! i just gettin started in java and come across these questions in previous exam papers and i couldnt do them

    Thanks again, any help with this would be great! :D


  • Registered Users Posts: 2,019 ✭✭✭Colonel Panic


    Is a reptile a mammal?

    Inheritance describes an is-a relationship.


  • Registered Users Posts: 4,376 ✭✭✭robbiezero


    mise1992 wrote: »
    While i have ye all here, on a different topic completely, class hierarchy.

    i have a parent class Animals with arrtibutes name, birth and speciecs. I have too child classes then Mammels and Reptiles with the same attributes as Animal along with NumLegs and whether they are a carnivore. However along ALL them Mammels have weight and height attributes and reptiles has an additional attribute of length. when i am writing me classes would i be ok to say:

    public abstract class Reptiles extend Mammels

    or do i need to say:

    public class Reptiles extends Animals


    Thanks everyone for the pervious answers! i just gettin started in java and come across these questions in previous exam papers and i couldnt do them

    Thanks again, any help with this would be great! :D


    I think what you need to do is this:
    public abstract class Animals

    then
    public class Reptiles extends Animals

    and
    public class Mammals extends Animals

    Animals need only be abstract if you want to ensure it is never instantiated i.e. if every animal is either a mammal or a reptile.


  • Registered Users Posts: 27,114 ✭✭✭✭GreeBo


    robbiezero wrote: »
    I think what you need to do is this:
    public abstract class Animals

    then
    public class Reptiles extends Animals

    and
    public class Mammals extends Animals

    Animals need only be abstract if you want to ensure it is never instantiated i.e. if every animal is either a mammal or a reptile.

    Seriously, read the thread.
    Dont just post the answer, it doesnt help anyone.

    The best way to help someone (imo) is to ask them leading questions and give them some clues, exactly as Colonel Panic did. If you figure something out for yourself you are far more likely to remember it and reuse it in similar situations (i.e. a pattern) because you understand it.


  • Registered Users Posts: 373 ✭✭jayo99


    tony81 wrote: »
    First, wrong answer.

    The op's problem stated "ask user how many values they want to enter and get the smallest number of the list of numbers"

    Also, I fail to see how giving the op an answer is at all helpful.

    I'm currently doing a very basic course in computers and we're plagued by people who have no idea, do irrelevant, non-working code, and get someone else in the class to "fix it up" for them.

    As a result there's rumours our C.A results are being withheld because they suspect plagiarism. They're going to wait until we do an open-book, class test to find out the people who "got help" (i.e. cheated in their assignments).

    LOL.. I see professore never responded :D


  • Advertisement
Advertisement