473,765 Members | 1,956 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
12 5503
r035198x
13,262 MVP
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 New Member
Thanks a lot!!!
Nov 2 '06 #12
r035198x
13,262 MVP
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
6438
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
4278
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
3208
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
2796
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
1399
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
9568
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
9399
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
10163
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...
0
10007
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9957
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
8832
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
6649
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
5423
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.