Jason2008
10-09-2008, 12:40 PM
Me and my friend tried doing these two java homeworks for couple of hours and we cannot figure it out. is there some one who can help me with these two exercises ?
They go as follows:
Ex 1.
Write an application that can hold five integers in an array. You can initialize the array elements with hard-coded values; you need not read them in from the keyboard. Display the integers from first to last (in terms of their order in the array, not sorted by value), and then display the integers from last to first (in terms of their order in the array).
Ex 2.
Write an application that than stores 10 prices in an array, such as $2.34, $7.89, $1.34, and so on. You can initialize the array elements with hard-coded values; you need not read them in from the keyboard. Display the sum of all the prices. Display all values less than $5.00. Calculate the average of the prices, and display all values that are higher than the calculated average value.
This is what we have so far: We are both newbies on the java class and so far for the first exercise I have problems printing the numbers from the last to the first. For the second exercise i have a bunch of problems with no ideas how to fix them =[. thank u for your help.
for exercise 1:
public class JavaArray {
/** Creates a new instance of JavaArray */
public JavaArray() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declare and create int array whose size is 5
int[] ages = {5,6,7,2,4};
// Display the value of each entry in the array
for( int i=0; i<ages.length; i++ ){
System.out.println ( "first to last number" + ages[i] );
for( int i=5; i<ages.length; i-- ){
System.out.println ( "first to last number" + ages[i] );
}
}
}
}
-------------------------------------------------------------------------------------------------------
for exercise 2:
/// to find sum
public class Arrays2{
public static void main(String args[]){
// this declares an array named fl with the type "array of int" and
// initialize its elements
float fl[] = {2.5f, 4.5f, 8.9f, 5.0f, 8.9f, 3.6f, 2.9f, 6.2f, 5.5f, 9.8f };
float sum = 0.0f;
for(int i=0; i<= 9; i++)
sum = sum + fl[i];
// displays the sum
System.out.println("The sum of the prices are= "+sum );
//find average
double average;
for(int i=0;i<10; i++)
{
sum+=fl[i];
}
average=sum/10;
System.out.println("Average is: "+average);
int lessThan = 5;
for (int lessThan = 1; lessThan < 5; i++) {
System.out.println ("less than 5 :" + lessthan);
}
}
}
}
They go as follows:
Ex 1.
Write an application that can hold five integers in an array. You can initialize the array elements with hard-coded values; you need not read them in from the keyboard. Display the integers from first to last (in terms of their order in the array, not sorted by value), and then display the integers from last to first (in terms of their order in the array).
Ex 2.
Write an application that than stores 10 prices in an array, such as $2.34, $7.89, $1.34, and so on. You can initialize the array elements with hard-coded values; you need not read them in from the keyboard. Display the sum of all the prices. Display all values less than $5.00. Calculate the average of the prices, and display all values that are higher than the calculated average value.
This is what we have so far: We are both newbies on the java class and so far for the first exercise I have problems printing the numbers from the last to the first. For the second exercise i have a bunch of problems with no ideas how to fix them =[. thank u for your help.
for exercise 1:
public class JavaArray {
/** Creates a new instance of JavaArray */
public JavaArray() {
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// Declare and create int array whose size is 5
int[] ages = {5,6,7,2,4};
// Display the value of each entry in the array
for( int i=0; i<ages.length; i++ ){
System.out.println ( "first to last number" + ages[i] );
for( int i=5; i<ages.length; i-- ){
System.out.println ( "first to last number" + ages[i] );
}
}
}
}
-------------------------------------------------------------------------------------------------------
for exercise 2:
/// to find sum
public class Arrays2{
public static void main(String args[]){
// this declares an array named fl with the type "array of int" and
// initialize its elements
float fl[] = {2.5f, 4.5f, 8.9f, 5.0f, 8.9f, 3.6f, 2.9f, 6.2f, 5.5f, 9.8f };
float sum = 0.0f;
for(int i=0; i<= 9; i++)
sum = sum + fl[i];
// displays the sum
System.out.println("The sum of the prices are= "+sum );
//find average
double average;
for(int i=0;i<10; i++)
{
sum+=fl[i];
}
average=sum/10;
System.out.println("Average is: "+average);
int lessThan = 5;
for (int lessThan = 1; lessThan < 5; i++) {
System.out.println ("less than 5 :" + lessthan);
}
}
}
}