1903 sample questions nov 26 2009

advertisement
ACS-1903
Sample Questions
Dec 2009
1. Complete the following code segment so that the larger of x and y is printed
Scanner kb = new Scanner(System.in);
x=kb.nextDouble();
y=kb.nextDouble();
// determine and display the larger of x and y
System.out.println (“The larger value is “+z);
2. Rewrite the following if-else structure as a switch statement
if (category == 1) discount = 0.20
else if (category == 2) discount = 0.25
else if (category == 4) discount =0.40
else discount = 0.10;
3. For what circumstance or situation do you need to write a method named
actionPerformed?
4. What does the acronym GUI represent?
5. Rewrite the following if-else structure using the conditional operator ?
if (category == 1) discount = 0.20
else discount = 0.25
6. Write the code necessary to exchange the first and last values of an array x.
For example, if x = {3,5,7,3,5,8,2,2,2,0} then after your code segment finishes
x = {0,5,7,3,5,8,2,2,2,3}.
7. Complete the code segment below so that the lines of MyFile.txt are displayed with
line numbers. If MyFile.txt contained the 3 lines:
This is a sample
file of three lines used for illustration
purposes
Then the output from your code segment is
1 This is a sample
2 file of three lines used for illustration
3 purposes
// Open the file.
FileReader fr = new FileReader("MyFile.txt");
BufferedReader if = new BufferedReader(fr);
1/6
ACS-1903
Sample Questions
Dec 2009
8. Suppose a user will use the keyboard to enter a line of text comprising a number of
words. Complete the code segment below so that it displays each word in lower case on
a separate line. If the line read from the keyboard is “A quick brown Fox” then the
output from your code segment is
a
quick
brown
fox
// Make the keyboard available
Scanner kb = new Scanner(System.in);
// Get the line
// display the words (tokens) one per line
9. What is the output from
int sum = 0;
int cc = 9025;
while (cc!=0)
{
sum = sum+cc%10;
System.out.print(sum+" ");
cc = cc/10;
System.out.println(cc);
}
Answer each of these questions using the pages that follow where you will find complete
programs with some comments. You must complete the required methods.
1. A variation on the dice game Pig is known as Hog. In Hog when a player takes a
turn the player declares how many dice he/she is throwing. If any one of the dice
shows a one, the player gets zero for that turn otherwise the player scores the total
showing on the dice. For example if a player declares to throw 5 dice and the 5
tosses are 3, 5, 2, 5, 3 then the player’s score for the turn is 3+5+2+5+3 = 18. If
the 5 tosses had been 3, 1, 6, 1, 2 then the player’s score for the turn is 0.
a. Write a method hogTurn() that implements a player’s turn in the game of
Hog. See the specifications given in the attached sheet.
b. Write a method scoreTurn() that calculates a player’s score for one turn.
See the specifications given in the attached sheet.
2/6
ACS-1903
Sample Questions
Dec 2009
2. Write a value-returning method, gpa(), that calculates a student’s grade point
average. The method has two parallel arrays as parameters: an array of grade
points, and an array of credit hours. Suppose for example a student has taken 5
courses and the two arrays are:
gradePoint = {4.0, 3.5, 3.0, 4.0, 4.5}
creditHours = {3, 6, 6, 3, 3}.
th
The i element of gradePoint is the grade point achieved for the ith course and the
ith element of creditHours is the credit hours associated with the ith course. A
grade point average is defined as the sum of the products of grade points and
credit hours divided by the sum of the credit hours. In this case we have:
(4.0*3+3.5*6+3.0*6+4.0*3+4.5*3) / (3+6+6+3+3) = 76.5/21 = 3.64
3. Write a value returning method average() that determines the average of numbers
that appear in a string. For example, if the string is “1.0 3.0 4.5 7.5” then the
average is (1.0 + 3.0 + 4.5 + 7.5) / 4 = 16.0/4 = 4.0
During the term we have used the Scanner class as an aid to get data from the
keyboard. The Scanner class can also be used with strings, as in the following:
String xyz = " LECTURES BEGIN on January 6 ";
Scanner s = new Scanner(xyz);
System.out.println(s.next());
// displays the token: LECTURES
System.out.println(s.hasNext()); // displays: true
3/6
ACS-1903
Sample Questions
Dec 2009
Answer for Programming Question 1
import java.util.Random;
/**
* Hog simulates one turn of the dice game Hog.
*/
public class Hog
{
// main() uses hogTurn() to throw the dice and scoreTurn() to get the score
public static void main()
{
int[] throw = hogTurn(10);
// play one turn with 6 dice
int score = scoreTurn(throw);
// obtain the score
for (int i=0;i<throw.length;i++) System.out.println(throw[i]); // display the throws
System.out.println(score
// display the score
}
/** hogTurn returns an array of integers representing the throws of n dice
* @param n the number of dice to be thrown
* @return an array of integers representing throws of dice
*/
public static int[] hogTurn(int n)
{
int[] throw = new int[n]; // to hold the throws of the dice
// your code goes here:
}
/** scoreTurn computes a players score for one turn of Hog
* @param toss an array of integers (throws of the dice)
* @return the player's score
*/
public static int scoreTurn(int[] throw)
{
int score = 0; // to keep the player's score
// examine the throws : if any throw is a 1, the score is 0
// otherwise the score is the sum of the throws
// your code goes here:
}
}
4/6
ACS-1903
Sample Questions
Answer for Programming Question 2
/**
* main() uses gpa() to calculate a gpa
*/
public class StudentGpa
{
public void main()
{
// the following displays a value of 3.642857142857143
double[] gradePoint = {4.0, 3.5, 3.0, 4.0, 4.5} ;
int[] creditHours = {3, 6, 6, 3, 3} ;
System.out.println(gpa(gradePoint, creditHours));
}
/**
* gpa calculates the gpa using two parallel arrays
*
* @param gradePoint array of grade points
* @param creditHours array of credit hours
* @return the student's gpa
*/
public double gpa(double[] gradePoint, int[] creditHours)
{
// your code goes here:
} }
5/6
Dec 2009
ACS-1903
Sample Questions
Answer for Programming Question 3
/**
* main() uses average() to calculate an average
*/
import java.util.Scanner;
/**
* Calculate the average of some numbers
*
*/
public class Average
{
public static void main()
{
Scanner s = new Scanner(System.in);
System.out.println("Enter some numbers then press enter");
String lineOfNumbers = s.nextLine();
System.out.println(average(lineOfNumbers));
}
/**
* average returns the average of numbers appearing in a string
* @param s the string containing numbers
* @return the average
*/
public static double average(String n)
{
// your code goes here:
} }
6/6
Dec 2009
Download