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. :confused:
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:
Code :
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.
Re: Could someone please help me figure out my homework please
Here is part 1:
Code :
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.");
}
}
}
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! :-bd
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.
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
Code :
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?
Re: Could someone please help me figure out my homework please
Quote:
Originally Posted by
napenthia
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
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?
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.
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?
Code :
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();
}
}
Re: Could someone please help me figure out my homework please
Code :
if (totalSize <=){
System.out.println();
}
Here is what's missing
Code :
(totalSize <= [B][I][U]100[/U][/I][/B])
It should look something like this:
Code :
if (totalSize <= 100){
// [I]do something[/I]
}
else{
// do something else
}
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