473,748 Members | 2,670 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

22 New Member
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 5501
r035198x
13,262 MVP
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 New Member
Im not even sure how to start any of them, this is what I have for #2:

import javax.swing.JOp tionPane;
public class KittyKitty {

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

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

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

import javax.swing.JOp tionPane;
public class KittyKitty {

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

do
{
System.out.prin tln ("Enter an integer: ");
result = "That number is " + ((num%2 == 0) ? "even" : "odd");
System.out.prin tln (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.JOp tionPane?
Have you done exception handling?

That I believe is all we need to get started on number one.
Nov 2 '06 #4
ryann18
22 New Member
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 MVP
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 MVP
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 New Member
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 MVP
What do you mean by
Where does the while loop go in this?
?
Nov 2 '06 #9
ryann18
22 New Member
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

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

Similar topics

0
6437
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 to achieve the result. Thankx in advance.
13
4277
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 not that good at programming but what I need to know is in java how would i do this.................. menu appears, user selects menu option (1,2,3,4,5,6) a new menu appears after selecting option 1 asking the user to select another option...
1
3206
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 inserted a little greeting in java (found free online), good morning, good afternoon... This is part of the code: hours = today.getHours(); if (hours<12) greeting = 'Good morning!'; if (hours<18 && hours>11) greeting = 'Good afternoon!';
10
2360
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 getItemTitle() location: class Product returntitle.compareTo(p.getItemTitle()); ^ Here is my code: // Product2 class import java.util.*;
2
2795
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
1398
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. For my last term I need to write a program that allows the user to play the game mastermind. I have a few problems that I can't seem to figure out... firstly I have an error and I don't know what it means: java.lang.NoClassDefFoundError: toets...
0
8995
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9561
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9332
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8252
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4608
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.