Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 12 of 12

Thread: Java program to prompt and display the user input number as odd or even

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Java program to prompt and display the user input number as odd or even

    1. Write a program that prompts the user to input an integer. The program should then output the number and a message saying whether the number is odd or even.

    2. Rectangle packages (boxes) may be sent by first class mail if their length plus girth is 100 inches or less. (Girth is distance around the middle of the package or package perimeter. You must calculate the perimeter in the algorithm.) Write a program to have the user input the length, width, and height of the parcel. Output the length, width, height, total girth, total size of the package (length plus girth), and whether or not the it may be sent by first class.

    Im not sure how to get started all i know for sure is the if then statements but thats about it.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Could someone please help me figure out my homework please

    Hello napenthia and welcome to the Java Programming Forums

    Both of these exercises will require you to use the Scanner class.

    Here is a code snippet for you:

    http://www.javaprogrammingforums.com...ner-class.html

    I will help you get started. I have written the code to prompt the user for an Integer value using the Scanner class:

    import java.util.Scanner;
     
    public class Class1 {
     
        public static void main(String[] args) {
     
            // Code in try/catch block to trap non int error
     
            try {
     
                // Prompt user for value
                System.out.println("Please enter an Integer value: ");
     
                // Declare the Scanner
                Scanner sc = new Scanner(System.in);
     
                // Set int variable
                int userInt = sc.nextInt();
     
                // Print result to console
                System.out.println("You entered the number: " + userInt);
     
            } catch (Exception e) {
                System.out.println("You must enter an Integer value!");
            }
     
        }
    }
    See how far you can get and post back when you get stuck.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    napenthia (April 21st, 2009)

  4. #3
    Junior Member
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Could someone please help me figure out my homework please

    Here is part 1:

    public class OddorEven {
     
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
            System.out.print("Please enter a whole number: ");
            Scanner s = new Scanner(System.in);
            int number = s.nextInt();
            s.close();
     
     
            System.out.print("You entered " + number + ". That number is ");
            if ((number % 2) == 0){
                System.out.print("even.");
            }
            else if ((number % 2) == 1){
                System.out.print("odd.");
            }        
        }
     
    }

  5. The Following User Says Thank You to dean_martin For This Useful Post:

    napenthia (April 21st, 2009)

  6. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Could someone please help me figure out my homework please

    Good work dean.

    Lets see if napenthia can work out part 2 on his own!
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #5
    Junior Member
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Could someone please help me figure out my homework please

    Pardon my duplication. I didn't refresh for awhile so I didn't see you already answered.

  8. #6
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Could someone please help me figure out my homework please

    ok I understood the first part and thank you for the help and what I have started with for the second part is this

     public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            int length, width, height, totalGirth, totalSize;
     
            System.out.println("Enter Length");
            length = scan.nextInt();
            scan.close();
    }

    so my questions are when i start to scan and imput the length do i have to close the scanner and then create a new one for each of the new one for the width and height, ect.?
    also should I be using Strings for this part as well?

  9. #7
    Junior Member
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Could someone please help me figure out my homework please

    Quote Originally Posted by napenthia View Post

    so my questions are when i start to scan and imput the length do i have to close the scanner and then create a new one for each of the new one for the width and height, ect.?
    also should I be using Strings for this part as well?
    1.You can reuse the scanner after declaring it once, because you are using it for int variables only.

    If (hypothetically)you needed to get a char or string, you need a char or string scanner. You don't need to

    close() it until you are finished using it. This program can probably get away with not closing the scanner,

    but you should get into the habit of closing your scanner, as it is considered a programming best practice.

    2.I don't see a need to use any String variables. No guarantees unless you post your assignment word for word.

    peace
    dean martin

  10. #8
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Could someone please help me figure out my homework please

    ok, so then how do i go about calculating the perimeter? Do I use the Math algorithm?

  11. #9
    Junior Member
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Could someone please help me figure out my homework please

    There is probably a Math method that does this for you, but this is faster for me than looking it up.

    int perimeter = (width + height * 2);
    int size = perimeter + length;

    There are some conceptual problems with determining the 'size' of a package this way. If a package is turned on it's side, so that the length is now the height, you would get a different 'size' for the same package. In my humble opinion, something like surface area or volume would be a more consistent measurement. But you should probably do it the way your professor wants.

  12. #10
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Could someone please help me figure out my homework please

    ok this is what i have so far but for the if, else statement it says that its an illegal start of an expression, is there a particular way I am suppose to implement an if, else statement?

     public static void main(String[] args) {
     
            Scanner scan = new Scanner(System.in);
            int length, width, height;
     
     
            System.out.println("Enter Length of Package");
            length = scan.nextInt();
     
            System.out.println("Enter Width of Package");
            width = scan.nextInt();
     
            System.out.println("Enter Hight of Package");
            height = scan.nextInt();
     
            int totalGirth = ((height* 2) + (width* 2));
            int totalSize = ((length) + (totalGirth));
     
            System.out.println("Length is: " + length);
            System.out.println("Width is: " + width);
            System.out.println("Height is: " + height);
            System.out.println("Total Girth is: " + totalGirth);
            System.out.println("Total Size of Package is: " + totalSize);
     
            if (totalSize <=){
                System.out.println();
     
            }
     
        }

  13. #11
    Junior Member
    Join Date
    Apr 2009
    Posts
    12
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Default Re: Could someone please help me figure out my homework please

    if (totalSize <=){
    System.out.println();
     
    }

    Here is what's missing
    (totalSize <= [B][I][U]100[/U][/I][/B])

    It should look something like this:

    if (totalSize <= 100){
    // [I]do something[/I]
    }
     
    else{
    // do something else
    }

  14. The Following User Says Thank You to dean_martin For This Useful Post:

    napenthia (April 22nd, 2009)

  15. #12
    Junior Member
    Join Date
    Apr 2009
    Posts
    25
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Smile Re: Could someone please help me figure out my homework please

    lol thanks, i was trying to figure out what i was missing and i completely over looked it, thanks so much for the help

Similar Threads

  1. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM
  2. [SOLVED] What is cast operator and how to use it?
    By napenthia in forum Java Theory & Questions
    Replies: 11
    Last Post: April 27th, 2009, 06:11 AM