MIDTERM 1 REVIEW – WRITING CODE POSSIBLE SOLUTION

advertisement
MIDTERM 1 REVIEW – WRITING CODE
POSSIBLE SOLUTION
1. Write a loop that computes (No need to write a complete program)
100 99 98 97
3
2
1


 ... 


1
2
3
4
98 99 100
Note: this is not the only solution;
double sum = 0;
double count = 1;
for (int k=100; k >=1; k--){
sum += k/count;
count ++;
}
2. Write a loop that displays the following pattern.
12345678987654321
234567898765432
3456789876543
45678987654
567898765
6789876
78987
898
9
public class TriangleOfNumbers {
public static void main(String[] args) {
int start = 1;
int end = 9;
//for each row
for(int row = 1; row <=9; row++){
//print the empty spaces
for (int blank = 1; blank < row; blank++)
System.out.print(" ");
//print numbers in ascending order
for (int i = start; i <=end; i++){
System.out.print(i);
}
//print numbers in descending order
for (int i=end-1; i >= start; i--){
System.out.print(i);
}
//go to next line
System.out.println();
//update the starting value
start++;
}
}
}
3. Write a program that prompts the user to enter an integer. If the number is a multiple of 5, print
HiFive. If the number is divisible by 2 or 3, print Georgia. Here are the sample runs:
Enter an integer: 6
Georgia
Enter an integer: 15
HiFive Georgia
Enter an integer: 25
HiFive
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter an integer: ");
int number = input.nextInt();
if (number % 5 == 0)
System.out.println("HiFive");
if (number % 2 == 0 || number % 3 == 0)
System.out.println("Georgia");
}
}
4.
Complete the code:
public class Test4 {
public static void main(String[] args) {
int i = 4; int j = 5;
// Fill in the code to invoke the sum method and display the sum of i and j.
System.out.println("The sum of " + i + " and " + j + " is " + sum(i,j));
}
public static int sum(int i, int j) {
// Fill in the code here to return the sum of i and j.
int result = i + j;
return result;
}
}
5.
Write a complete program that asks the user to enter in an unspecified number of integers
between 1 and 100 and count the occurrences of each. You can assume the input will end when
the user enters a 0. Then present a summary report like the following (output below is based on
the user inputting the following numbers: 2 5 6 5 4 3 23 43 2 0)
Possible output:
The
The
The
The
The
The
The
The
number
number
number
number
number
number
number
number
0 occurred 1 time(s)
2 occurred 2 time(s)
3 occurred 1 time(s)
4 occurred 1 time(s)
5 occurred 2 time(s)
6 occurred 1 time(s)
23 occurred 1 time(s)
43 occurred 1 time(s)
public class Occurrence {
public static void main(String args[]){
//create a scanner
Scanner nextInteger = new Scanner(System.in);
//prompt the user for integers
int entered = -1000;
//create an array that can contain count info for any number between 0 and 100
int[] integerArray = new int[101];
while (entered !=0){
System.out.println("please enter an integer between 1 and 100,
enter 0 to exit");
entered = nextInteger.nextInt();
for (int i=0; i<integerArray.length; i++){
if (entered ==i){
integerArray[i]++;
}
}
}
for (int i=0; i<integerArray.length; i++){
if (integerArray[i] != 0){
System.out.println("The number " + i + " occurred " +
integerArray[i] + " time(s) ");
}
}
}
}
6.
Given this method:
static void nPrint(String message, int n) {
while (n > 0)
{
System.out.print(message);
n--;
}
}

What is the printout of the call
nPrint("a", 4)?


What is k after invoking the following code?
int k = 2;
nPrint("A message", k);
aaaa
A messageA message
after call: 2
7.
Write a program that reads an unspecified number of scores and determines how many
scores are above or equal to the average and how many scores are below the average. Enter a
negative number to signify the end of the input. Assume that the maximum number of scores is
10 (i.e. you can end the program after collecting 10 numbers even if you don’t encounter a
negative number)
import java.util.Scanner;
public class Scores {
public static void main(String[] args) {
//create a scanner
Scanner input = new Scanner(System.in);
// count represents number of entered numbers
int count = 0;
//declare array of scores for storing the scores
double[] scoreArray = new double[10];
//declare and initialize temp and sum
double temp = 0;
double sum = 0;
//prompt the user for up to 10 grades
while (count < scoreArray.length){
System.out.println("please enter your score, enter negative to exit");
temp = input.nextDouble();
//if user enters positive number, store the number; update count and sum
if (temp >= 0){
scoreArray[count] = temp;
sum += scoreArray[count];
count++;
}
//if user enters negative number break
else break;
}
//compute average
double average = sum/count;
double lower = 0;
//count number of scores below average
for (int i=0; i<count; i++){
if (scoreArray[i] < average)
lower ++;
}
//display results
System.out.println("The average is: " + average);
System.out.println("There are " +(int) (count-lower) + " grades above or
equal average");
System.out.println("There are " + (int) lower + " grades below
average");
}
}
8.
Write a method that checks to see if a list contains 4 consecutive values in a row. For
example, the following list contains four “2′s” in a row:
int[] x = {1,2,2,2,2,3,4,5};
Here’s a sample program to get you started – complete the method implementation:
public static void main(String[] args) {
int[] a = { 2, 3, 4, 4, 4, 2, 2, 2, 2, 1, 2, 3, 4, 4, 2, 2, 2 };
System.out.println( isConsecutiveFour(a) ); // will print true
}
public static boolean isConsecutiveFour(int[] values){
int count =1;
//compare consecutive values;
for (int i =0; i<values.length-1; i++){
//if consecutive values are equal increment the counter
if (values[i]== values[i+1])
count ++;
//if are not equal reset counter to 1
else
count=1;
//if 4 consecutive values return true
if (count == 4)
return true;
}
return false;
}
9. Write a complete program that asks the user to enter in 10 integers and then displays them in the
reverse order in which they were read.
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
//declare an array of integers of size 10
int[] numbers = new int[10];
//create a scanner
Scanner integer = new Scanner(System.in);
//store prompted values into array numbers
for (int i=0; i<numbers.length; i++){
System.out.println("Please enter an integer ");
numbers[i]= integer.nextInt();
}
//print content of entered numbers in reverse order
for (int j = numbers.length -1; j>=0; j--){
System.out.print(numbers[j] +
" ");
}
}
}
10.
Write a program that prompts the user to enter the number of student’s, the students’ names,
and their scores and prints student name in decreasing order of their scores.
Note: from textbook
public class Student{
public static void main(String[] args) {
//create a scanner for numeric data
java.util.Scanner input = new java.util.Scanner(System.in);
// create a scanner for string data
java.util.Scanner inputString = new java.util.Scanner(System.in);
// Prompt the user to enter the number of students
System.out.print("Enter the number of students: ");
int numberOfStudents = input.nextInt();
String[] names = new String[numberOfStudents];
double[] scores = new double[numberOfStudents];
// Enter student name and score
for (int i = 0; i < scores.length; i++) {
System.out.print("Enter a student name: ");
names[i] = inputString.next();
System.out.print("Enter a student score: ");
scores[i] = input.nextDouble();
}
// Sort
for (int i = 0; i < numberOfStudents - 1; i++) {
// Find the minimum in the scores
double currentMin = scores[i];
int currentMinIndex = i;
for (int j = i + 1; j < numberOfStudents; j++) {
if (currentMin > scores[j]) {
currentMin = scores[j];
currentMinIndex = j;
}
}
// Swap scores[i] with scores[currentMinIndex] if necessary;
// Swap names[i] with names[currentMinIndex] if necessary;
if (currentMinIndex != i) {
scores[currentMinIndex] = scores[i];
scores[i] = currentMin;
String temp = names[currentMinIndex];
names[currentMinIndex] = names[i];
names[i] = temp;
}
}
// Print names and scores
for (int i = scores.length - 1; i >= 0; i--) {
System.out.println(names[i] + "\t" + scores[i]);
}
}
}
Download