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).
12 5501
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.
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);
}
}
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.
I'm using JDK1.5 with netbeans, nah I'm not familiar and I've never used that.
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. - import javax.swing.*;
-
public class Number1 {
-
public static void main(String[] args) {
-
String input = JOptionPane.showInputDialog(null, "Enter a number");
-
int i = input.length() - 1;
-
boolean valid = true;
-
while(i > 0) {
-
if(!Character.isDigit(input.charAt(i))) {
-
valid = false;
-
break;
-
}
-
i--;
-
}
-
if(!valid) {
-
JOptionPane.showMessageDialog(null, input+" is not a positive integer");
-
}
-
else {
-
JOptionPane.showMessageDialog(null, input+" is a positive integer");
-
}
-
}
-
}
Of course moving back one decade, we have the non-swing solution - import java.util.Scanner;
-
public class Number1 {
-
public static void main(String[] args) {
-
Scanner scan = new Scanner(System.in);
-
System.out.print("Enter your input and press Enter: ");
-
String input = scan.nextLine();
-
int i = input.length() - 1;
-
boolean valid = true;
-
while(i > 0) {
-
if(!Character.isDigit(input.charAt(i))) {
-
valid = false;
-
break;
-
}
-
i--;
-
}
-
if(!valid) {
-
System.out.println(input+" is not a positive integer");
-
}
-
else {
-
System.out.println(input+" is a positive integer");
-
}
-
}
-
}
-
-
-
Of course moving back one decade, we have the non-swing solution - import java.util.Scanner;
-
public class Number1 {
-
public static void main(String[] args) {
-
Scanner scan = new Scanner(System.in);
-
System.out.print("Enter your input and press Enter: ");
-
String input = scan.nextLine();
-
int i = input.length() - 1;
-
boolean valid = true;
-
while(i > 0) {
-
if(!Character.isDigit(input.charAt(i))) {
-
valid = false;
-
break;
-
}
-
i--;
-
}
-
if(!valid) {
-
System.out.println(input+" is not a positive integer");
-
}
-
else {
-
System.out.println(input+" is a positive integer");
-
}
-
}
-
}
-
-
-
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??
What do you mean by
Where does the while loop go in this?
?
What do you mean by ?
Sorry I meant is there any way I can only do a while without an if / else statement?
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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!';
|
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.*;
|
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).
| |
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
| |
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |