Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java help

 
richie o donoghue
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi i was wonderin if any 1 could help me with these problems???Write a program that accepts 3 integer values and outputs them in ascending order.
a) Write a program that outputs the numbers from 30 to 1 (in descending order).

b) Write a program that outputs the numbers from 0 to 20 and their squares, i.e., program output should look like this:

The square of 0 is 0.
The square of 1 is 1.
The square of 2 is 4.
:
:
The square of 20 is 400.

c) Write a program that asks the user for a positive number between 1 and 100 inclusive. If the user enters a valid number, the program then outputs a list of the even numbers between 1 and that number inclusive; if the user enters an invalid number, the program outputs an error message.

 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Since these sound like homework problems, I wouldn't be expecting to see any code here. Since you haven't posted code to show that you have at least attempted the problems, I wouldn't expect to get much help until you do.
If you are just completely stuck at where to start, it should be fairly obvious that some kind of flow control is needed here, more specifically a looping construct.
If you want to show some code that you have, it might help us to better address where you are having problems.
Jason
 
richie o donoghue
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class WriteValues
{
public static void main (String[] args)
{
int count = 1;
while (count <= 30)<br /> {<br /> System.out.println ("The value of count is now " + count);<br /> count = count + 1;<br /> }<br /> }<br /> }<br /> I DONT KNOW IF THAT WILL WRITE THEM OUT IN DESCENDING ORDER.THIS WAS MY ATTEMPT.<br /> THE FIRST PART TO ENTER 3 INTEGERS I TYPED-public class AscendingOrder<br /> {<br /> public static void main (String[] args)<br /> {<br /> int integer1;<br /> int integer2;<br /> int integer3;<br /> System.out.println ("Please enter an integer and press return");<br /> integer1 = Reader.readInt;<br /> System.out.println ("Please enter a second integer and press return");<br /> integer2 = Reader.readInt;<br /> System.out.println ("Please enter a third integer and press return");<br /> integer3 = Reader.readInt;<br /> <br /> if (integer1 > integer2 > integer3)
System.out.println

AND IM STUCK ON THE SQUARE ROOTS THING???
HELP
 
Junilu Lacar
Bartender
Pie
Posts: 7306
45
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>> Originally posted by richie o donoghue:
>> I DONT KNOW IF THAT WILL WRITE THEM OUT IN DESCENDING ORDER.THIS WAS MY ATTEMPT
Well, did you attempt to run the program and see what it printed out? Does it print out 30, 29, 28, ... 1 (correct solution) or 1, 2, 3, ...30 (incorrect solution)?

>> AND IM STUCK ON THE SQUARE ROOTS THING???
HELP
Well, first decide which one you want: do you want the SQUARE of a number (which you can calculate by multiplying the number by itself) or the SQUARE ROOT (in which case, you'd probably want to pass the number to the java.lang.Math.sqrt() method)

Good luck,
Junilu
 
Biju Philip
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a quick example of your square root task. There are many different variations of this task... you may wish to review iteration in Java (using "for" and "while" loops). Sorry if the formatting is a bit off.
<CODE>
//package name here
/**
* A simple class to create squares of integers.
*/
public class SquareRootGen {
/**
* Creates a new <code>SquareRootGen</code> instance.
*/
public SquareRootGen (){
}

/**
* <code>main</code> method that does the work
*/
public static void main(String args []) {
// create your max integer value
int max = 21;
// create a square root value
int j = 0;
// iterate through starting from zero to one int less than the max value
// it will start from zero and end at 20
for (int i = 0; i < max; i++) {
// assign the value of i times i to j
j = i*i;
// print your statement
System.out.println("The square of " +i +" is " +j +".");
}
}
}// SquareRootGen
</CODE>

[This message has been edited by Biju Philip (edited December 20, 2001).]
[This message has been edited by Biju Philip (edited December 20, 2001).]
[This message has been edited by Biju Philip (edited December 20, 2001).]
 
Dave Vick
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biju
You can fix the formatting of the code you post by enclosing it in UBB code tags. See this page: http://www.javaranch.com/ubb/ubbcode.html
Then you can output things like this:


------------------
Dave
Sun Certified Programmer for the Java� 2 Platform
 
Shubhrajit Chatterjee
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
I don't mean to be rude but I have a few things to say about homework problems.
These problems are fairly simple and if a student is not able to do it by himself then a lot of help should be around. When I was in college, I would always go to my classmates and ask them for their help. That is I feel a much better way to learn. Next he can consult his mentorsand professors. I think these forums are the last place where he should post and definitely not in an (intermediate java)forum. There is a beginners forum also.
------------------
Shubhrajit
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic