dcsimg
I have a few Java Questions
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Saturday, March 18, 2006 01:09 PM

Firstly, these 3 questions are part of an assignment which I tried doing myself. I keep on running into walls trying to solve it. Hence, could you help me please? I am quite new to java and was wondering how do I complete building this program. The objective of the program is to randomly generate 100 2 digit odd integer values. I can generate the values but I don't know how to sieve only 100 odd integers. I am also supposed to find the maximum, minimum and average of all the numbers generated. I can find the average but can't seem to find maximum and minimum. I also need to point out the location of the maximum and minimum which I do not have a clue on how to do.    More>>

Firstly, these 3 questions are part of an assignment which I tried doing myself. I keep on running into walls trying to solve it. Hence, could you help me please?


I am quite new to java and was wondering how do I complete building this program.

The objective of the program is to randomly generate 100 2 digit odd integer values.
I can generate the values but I don't know how to sieve only 100 odd integers.

I am also supposed to find the maximum, minimum and average of all the numbers generated. I can find the average but can't seem to find maximum and minimum.

I also need to point out the location of the maximum and minimum which I do not have a clue on how to do.

Lastly, my program is suppose to loop when keyed in 'y' or 'Y'. I think I have done that.

My codes are as below

			
import java.io.*;

class Question2
{

public static void main (String args []) throws IOException
{
BufferedReader stdin = new BufferedReader (new InputStreamReader(System.in));

Question2 theClass = new Question2();

String strValue = "";
int value = 0;
int random [] = new int [100];
int l= random.length;
int max=0, min=0;
int sum = 0;

do {
System.out.print("Enter 'Y' to Start / Begin ");
strValue = stdin.readLine();
if (strValue.equalsIgnoreCase("Y")){

for(int i = 0; i
{


if (i%10==0)
{
System.out.println("");
}
random[i] = randomNumber(value);//recall method to generate numbers

System.out.print(random[i] + " ");

int maximum = random[i];
max = findMaximum(maximum);//recall method for maximum

int minimum = random[i];
min = findMinimum(minimum);//recall method for minimum

sum = sum + random[i];

}


}
else
break;

int average=0;
average = findAverage(sum);//recall method for average
System.out.println("
The average is " + average);
System.out.println("The Maximum Number is " + max);
System.out.println("The Minimum Number is " + min);

}while (strValue.equalsIgnoreCase("Y"));
}
//random number generator
public static int randomNumber (int value)
{

int choose=0;

choose = 10 + (int)(Math.random()*89);

return choose;
}
//maximum number generator
public static int findMaximum(int maximum)
{
int maxi = 0;
if (maximum>maxi)
maxi=maximum;

return maxi;

}

//average generator
public static int findAverage (int sum)
{
int avg = 0;

avg = sum/100;

return avg;
}

//minimum generator
public static int findMinimum(int minimum)
{
int mini = 0;

if (minimum
mini = minimum;

return mini;
}
}


and
secondly,

I have been given the task of finding the sum, quotient and remainder of two input integers. Not that much of a problem.

But the problem now is, I am supposed to display the digits entered with the numbers being separated by two spaces.
In example, if the number is 5678
the display should be

5 6 7 8

How?

			
import javax.swing.*;

class Question1 {

public static void main (String args[]){
Question1 theObject = new Question1();

String strA = "";
String strB = "";

int a = 0;
int b = 0;
int qoutient = 0;
int remainder = 0;


for ( ; ;)
{
strA = JOptionPane.showInputDialog("Please enter 1st integer : ");
a = Integer.parseInt(strA);
if (a==0)
break;

strB = JOptionPane.showInputDialog("Please enter 2nd Integer : ");
b = Integer.parseInt(strB);
if (b==0)
break;

qoutient = a / b;
remainder = a % b;

int displayA = displayDigits(a);
int displayQoutient = displayDigits(qoutient);
int displayB = displayDigits(b);
int displayRemainder = displayDigits(remainder);


JOptionPane.showMessageDialog(null, "The Digit a is "+ displayA + "
The Digit B is " + displayB
+"
The quotient is " + displayQoutient + "
The Remainder is " + displayRemainder,
"Digits Displayed", JOptionPane.INFORMATION_MESSAGE);
}
}//end main method

public static int displayDigits(int number){


int placeValue = number % 10;
number = number / 10;
int reverseNumber = 0;
reverseNumber = reverseNumber * 10 + placeValue;

return reverseNumber;



}
}


Thirdly,


The program below is suppose to run the ammount of people within a certain salary range. The user needs to key in the salary and the counter must go up.

for instance when a person keys in 200

then in the range of 200-299 there'll be one.

Is this correct?

The program is supposed to stop when a negative number is keyed in

			
import javax.swing.*;

class Question3
{

public static void main (String args [])
{
int salaryA [] = new int [999];
int salaryB [] = new int [999];
int salaryC [] = new int [999];
int salaryD [] = new int [999];
int salaryE [] = new int [999];
int salaryF [] = new int [999];
int salaryG [] = new int [999];
int salaryH [] = new int [999];
int salaryI [] = new int [999];


String strSlry = "";
int slry = 0;
int i = 0;

JTextArea theText = new JTextArea(null, 11, 2);

for(;;)
{
strSlry = JOptionPane.showInputDialog(null, "Please Enter Salary");
slry = Integer.parseInt(strSlry);

if ((slry>=200)&&(slry <300))
{
for (; i
{
salaryA[i]+=slry;
}
}

if ((slry>=300)&&(slry <400))
{
for (; i
{
salaryB[i]+=slry;
}
}

if ((slry>=400)&&(slry <500))
{
for (; i
{
salaryC[i]+=slry;
}
}

if ((slry>=500)&&(slry <600))
{
for (; i
{
salaryD[i]+=slry;
}
}

if ((slry>=600)&&(slry <700))
{
for (; i
{
salaryE[i]+=slry;
}
}

if ((slry>=700)&&(slry <800))
{
for (; i
{
salaryF[i]+=slry;
}
}

if ((slry>=800)&&(slry <900))
{
for (; i
{
salaryG[i]=slry;
}
}

if ((slry>=900)&&(slry <1000))
{
for (; i
{
salaryH[i]+=slry;
}
}

if (slry>=1000)
{
for (; i
{
salaryI[i]+=slry;
}
}

if ((slry> <200)&&(slry>0))
JOptionPane.showMessageDialog(null, "Invalid Salary");

if (slry <0)
break;
}

theText.append("Salary Range($) Number of Staff(s)");
theText.append("
--------------------------------------");
theText.append("
200 - 299 " + salaryA[i]);

JOptionPane.showMessageDialog(null, theText, "Salary Table", JOptionPane.INFORMATION_MESSAGE);
}//end method
}//end class



why doesn't it compile properly and the error message is out of bounds.

Thank you very much....

   <<Less

Re: I have a few Java Questions

Posted By:   Almagest_FUTT  
Posted On:   Monday, March 20, 2006 06:25 AM

The objective of the program is to randomly generate 100 2 digit odd integer values. I can generate the values but I don't know how to sieve only 100 odd integers.

In each step:

10: generate a random number

20: if it's even, goto 10

30: keep the result.


I can find the average but can't seem to find maximum and minimum.

That's because the "maxi" and "mini" variables you have in you static methods are local variables, and thence get lost as soon as you've left the block.

Do it differently: put your array of random numbers together, and then search it for the hightest and the lowest value. In fact, you don't even need a seperate method for this task: you have the actual maximum and minimum in your main loop, so you can compare the new value against these.


But the problem now is, I am supposed to display the digits entered with the numbers being separated by two spaces. In example, if the number is 5678 the display should be

5 6 7 8

How?


int num = ...;
String likeyouwant = Integer.toString(num).replaceAll("\d", "$0 ");

or

int num = ...;
char[] chars = Integer.toString(num).toCharArray();
StringBuffer sb = new StringBuffer();
for(int ii = 0; ii < chars.length; ii++){
sb.append(chars[ii]).append(' ').append(' ');
}
String likeyouwant = sb.toString();



Thirdly,

The "salary" variables must either each be a simple int, and then in the loops you do:

if ((slry>=900)&&(slry<1000)){
salaryH ++;
}

or you put only one int array, the size of which is the number of ranges. For each number input, you find the index, in that array, of the range the number belongs to, and increment the integer at that position. The second version is a bit cleaner, methinks.
About | Sitemap | Contact