Download solution

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
ARRAYS
CLASS EXERCISE 1
SOLUTION
1.
Write a program that reads in a set of integers from the user, storing them in an array, then prints them
out in reverse order. ASSUME 10 NUMBERS…
public class ReverseNumbers
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args)
{
//
DECLARE & CREATE ARRAY
int[] numbers = new int[10];
System.out.println(“The size of the array: “ + numbers.length);
//
PROMPT USER FOR ARRAY VALUES
for (int index = 0; index < numbers.length; index++)
{
System.out.print(“Enter number “ + (index + 1) + “: “);
numbers[index] = Integer.parseInt(keyboard.readLine());
}
//
DISPLAY CONTENTS OF ARRAY IN REVERSE ORDER
System.out.println(“The numbers in reverse: “);
for (int index = number.length-1; index >= 0; index--)
System.out.print(numbers[index] + “
System.out.println();
}
}
1
“);
2.
Write a program that prompts the user to make a choice for a pizza size S, M, L or X – and then displays the price as R6.99, R8.99, R12.50 or R15.00 accordingly. You will
need to create 2 arrays!!
public class Choice
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
public static void main (String args[]) throws Exception
{
char[] sizes = {'S','M','L','X'};
double[] prices = {6.99,8.99,12.50,15.00};
char selection;
System.out.print("Enter pizza size (S,M,L,X): ");
selection = (keyboard.readLine()).charAt(0):
for (int index = 0; index < 4; index++)
if (selection == sizes[index])
{
System.out.println("The price is " + prices[index]);
return;
}
System.out.println("Invalid Entry");
}
}
2
3.
Create an array that stores 20 prices, such as 2.34, 7.89, 1.34, and so on. Display the sum of all the
prices, and display all values higher than the calculated average value.
public class Prices
{
public static void main (String args[])
{
//
DECLARE & CREATE ARRAY
double[] prices = {2.34,7.89,3.55,6.99,8.99,2.39,
1.29,11.88,56.35,12.33,4.50,1.19,1.79,
2.59,7.10, 9.99,4.55,12.39,4.44,5.55};
double average;
double total = 0.0;
//
CALCULATE TOTAL
for (index = 0; index < prices.length; index++)
{
total += prices[index];
}
System.out.println(“Sum of all the prices = “ + total);
//
CALCULATE AVERAGE
average = total / prices.length
System.out.println("\nThe average price is " + average + "\n");
//
DISPLAY VALUES HIGHER THAN AVERAGE
for (index = 0; index < 20; index++)
{
if (prices[index] > average)
System.out.println(prices[i] + " is greater than the average");
}
}
}
3
4.
public class AssignGrade
{
static BufferedReader keyboard =
new BufferedReader(new InputStreamReader(System.in));
// MAIN METHOD
public static void main(String[] args)
{
int numOfStudents = 0;
// THE NUMBER OF STUDENTS
int[] scores;
// ARRAY SCORES
int best = 0;
// THE BEST SCORE
char grade;
// THE GRADE
// GET NUMBER OF STUDENTS
System.out.println(“Please enter number of students”);
numOfStudents = Integer.parseInt(keyboard.readLine());
// CREATE ARRAY SCORES
scores = new int[numOfStudents];
// READ SCORES AND FIND THE BEST SCORE
System.out.println(“Please enter “ + numOfStudents + “ scores”);
for (int index = 0; index < scores.length; index++)
{
scores[index] = Integer.parseInt(keyboard.readLine());
if (scores[index] > best)
best = scores[index];
}
// ASSIGN AND DISPLAY GRADES
for (index = 0; index < scores.length; index++)
{
if (scores[index] >= best –10)
grade = ‘A’;
else if (scores[index] >= best –20)
grade = ‘B’;
else if (scores[index] >= best –30)
grade = ‘C’;
else if (scores[index] >= best –40)
grade = ‘D’;
else
grade = ‘F’;
System.out.println(“Student “ + index + “ score is “
+ scores[index] + “ and grade is “ + grade);
}
}
}
4
NOTE:
The scores is declared as an array of int type in order to store scores. At the time this array is declared,
its size is undetermined. After the user enters the number of students into numOfStudents, an array with
the size of numOfStudents is created.
The array is not needed to find the best score, but it is needed to keep all of the scores so that grades can be
assigned later on, and it is needed when scores are printed along with the students’ grades.
5