473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Java exercises PLEASE help...should be easy for most java users!!!!

22
1. Write a while loop that verifies that teh user enters a positive integer value.

2. Write a do loop that verifies that teh user enters an even integer value.

3. Write a for loop to print the odd numbers from 1 to 99 (inclusive).

4. Write a for loop to print the multiples of 3 from 300 down to 3.

5. Write a method called alarm that prints the string "Alarm!" multiple times on
seperate lines. The method should accept an integer parameter that
specifies how many times the string is printed. Print an error message if
the parameter is less than 1.

6. Write a method called sumRange that accepts two integers parameters that
represent a range. Issue an error message and return zero if the second
parameter is less than the first. Otherwise, the method should return the
sum of integers in that range (inclusive).
Nov 2 '06 #1
12 5465
r035198x
13,262 8TB
1. Write a while loop that verifies that teh user enters a positive integer value.

2. Write a do loop that verifies that teh user enters an even integer value.

3. Write a for loop to print the odd numbers from 1 to 99 (inclusive).

4. Write a for loop to print the multiples of 3 from 300 down to 3.

5. Write a method called alarm that prints the string "Alarm!" multiple times on
seperate lines. The method should accept an integer parameter that
specifies how many times the string is printed. Print an error message if
the parameter is less than 1.

6. Write a method called sumRange that accepts two integers parameters that
represent a range. Issue an error message and return zero if the second
parameter is less than the first. Otherwise, the method should return the
sum of integers in that range (inclusive).
You start.
Nov 2 '06 #2
ryann18
22
Im not even sure how to start any of them, this is what I have for #2:

import javax.swing.JOptionPane;
public class KittyKitty {

public static void main (String[] args)
{
String numStr, result;

do
{
System.out.println ("Enter an integer: ");
result = "That number is " + ((num%2 == 0) ? "even" : "odd");
System.out.println (result);

}
while (result > 0);
}
}
Nov 2 '06 #3
r035198x
13,262 8TB
Im not even sure how to start any of them, this is what I have for #2:

import javax.swing.JOptionPane;
public class KittyKitty {

public static void main (String[] args)
{
String numStr, result;

do
{
System.out.println ("Enter an integer: ");
result = "That number is " + ((num%2 == 0) ? "even" : "odd");
System.out.println (result);

}
while (result > 0);
}
}
No that is very bad. Now let's start with number 1, but first a little info and we will be underway.
Are you using jdk1.5 or jdk1.4?
Are familiar with javax.swing.JOptionPane?
Have you done exception handling?

That I believe is all we need to get started on number one.
Nov 2 '06 #4
ryann18
22
I'm using JDK1.5 with netbeans, nah I'm not familiar and I've never used that.
Nov 2 '06 #5
r035198x
13,262 8TB
I'm using JDK1.5 with netbeans, nah I'm not familiar and I've never used that.
Here are the swing components in action. Notice that your program lacked a way of taking in input from the user. Now be honest and say where you do not understand in this code.

Expand|Select|Wrap|Line Numbers
  1. import javax.swing.*;
  2. public class Number1 {
  3.     public static void main(String[] args) {
  4.         String input = JOptionPane.showInputDialog(null, "Enter a number");
  5.         int i = input.length() - 1;
  6.         boolean valid = true;
  7.         while(i > 0) {
  8.             if(!Character.isDigit(input.charAt(i))) {
  9.                 valid = false;
  10.                 break;
  11.             }
  12.             i--;
  13.         }
  14.         if(!valid) {
  15.             JOptionPane.showMessageDialog(null, input+" is not a positive integer");
  16.         }
  17.         else {
  18.             JOptionPane.showMessageDialog(null, input+" is a positive integer");
  19.         }
  20.     }
  21. }
Nov 2 '06 #6
r035198x
13,262 8TB
Of course moving back one decade, we have the non-swing solution

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Number1 {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         System.out.print("Enter your input and press Enter: ");
  6.         String input = scan.nextLine();
  7.         int i = input.length() - 1;
  8.         boolean valid = true;
  9.         while(i > 0) {
  10.             if(!Character.isDigit(input.charAt(i))) {
  11.                 valid = false;
  12.                 break;
  13.             }
  14.             i--;
  15.         }
  16.         if(!valid) {
  17.             System.out.println(input+" is not a positive integer");
  18.         }
  19.         else {
  20.             System.out.println(input+" is a positive integer");
  21.         }
  22.     }
  23. }
  24.  
  25.  
  26.  
Nov 2 '06 #7
ryann18
22
Of course moving back one decade, we have the non-swing solution

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Number1 {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         System.out.print("Enter your input and press Enter: ");
  6.         String input = scan.nextLine();
  7.         int i = input.length() - 1;
  8.         boolean valid = true;
  9.         while(i > 0) {
  10.             if(!Character.isDigit(input.charAt(i))) {
  11.                 valid = false;
  12.                 break;
  13.             }
  14.             i--;
  15.         }
  16.         if(!valid) {
  17.             System.out.println(input+" is not a positive integer");
  18.         }
  19.         else {
  20.             System.out.println(input+" is a positive integer");
  21.         }
  22.     }
  23. }
  24.  
  25.  
  26.  
Where does the while loop go in this?

For the second one would I just do basically the same thing except add num%2 for it to become even??
Nov 2 '06 #8
r035198x
13,262 8TB
What do you mean by
Where does the while loop go in this?
?
Nov 2 '06 #9
ryann18
22
What do you mean by ?
Sorry I meant is there any way I can only do a while without an if / else statement?
Nov 2 '06 #10
r035198x
13,262 8TB
Sorry I meant is there any way I can only do a while without an if / else statement?
Not for this one. You have to test that the input satisfies the given conditions and the if/else is tailor made for that.
Now in that Number1, the while condition should read while(i >= 0) for reasons that should now be obvious. I have to knock off to bed now (9:54pm here).

Number 2 does not say the number should be positive. So my solution includes - sign at the start of the number. Here is how you would do it. Now hopefully by tommorrow you will have walked through the rest of the exercises.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. public class Number1 {
  3.     public static void main(String[] args) {
  4.         Scanner scan = new Scanner(System.in);
  5.         System.out.print("Enter your input and press Enter: ");
  6.         String input = scan.nextLine();
  7.         int i = input.length() - 1;
  8.         boolean valid = true;
  9.         do {
  10.             if(i == 0) {
  11.                 if(!(Character.isDigit(input.charAt(i)) || input.charAt(i) == '-')) {
  12.                     valid = false;
  13.                     break;
  14.                 }
  15.             }
  16.             else if(!Character.isDigit(input.charAt(i))) {
  17.                 valid = false;
  18.                 break;
  19.             }
  20.             i--;
  21.         }
  22.         while(i >= 0);
  23.         if(!valid) {
  24.             System.out.println(input+" is not a Number");
  25.         }
  26.         else {
  27.             int integer = Integer.parseInt(input);
  28.             if(integer % 2 ==0) {
  29.                 System.out.println(input+" is a positive even number");
  30.             }
  31.             else {
  32.                 System.out.println(input+" is not even");
  33.             }
  34.         }
  35.     }
  36.  
  37.  
  38. }
Nov 2 '06 #11
ryann18
22
Thanks a lot!!!
Nov 2 '06 #12
r035198x
13,262 8TB
Thanks a lot!!!
Hope you finish up the rest on the exercises.
Nov 3 '06 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sarith sutha | last post by:
Hi Need a help on the above topic, how to pass a java object as parameter to the java stored procedure .Code snippets will be greatly appreciated , i came across udf/udts etc but still not able...
13
by: Andrew Bell | last post by:
I'm doing a uni course in the UK and one of my semesters is about programming. This is just going to be compilied and executed with no menu just using command promt (javac classfile.class) I am...
1
by: nela | last post by:
Hello everyone, from Croatia ! I am designing a site for a Serbian client, and the english page is ok, but the serbian page needs to be cyrillic. It was no problem for Photoshop design, but I...
10
by: ITQUEST | last post by:
I am on the second step of my Inventory Project and can properly run my Inventory2.java file but not my Product2.java file. I get the following error: 71:cannot find symbol, symbol: method...
2
by: sunsom | last post by:
hi all, i have a requirement to send FAX through java code. how can i do a fax in java..kindly help!!!. what API is there for FAX....(free API).
4
by: Nadiatjie | last post by:
I am still in school and studying java, I am kind of new to it(I have been working in Delphi for 2 years and only recently started working with java...) as well and really need help with a project....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.