Download import java.util.Scanner

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
Marking scheme-CS1050_Quiz
Question 1.1
Bug
1
2
3
4
5
6
# Line #
6
7
9
11
12
14
Corrected Bug
StarWars  starWars()
add semicolon at end of line
String[ ] String
.length() .length
<=<
[i]  [j]
(6 points)
Question 1.2
(4 points)
ew0ks!
Question 2
// 2 points reserved for especially elegant/clear AND correct solutions
static double[] partialRandomArray (int n) {
double[] result = new double[2 * n];
for (int i = 0; i < result.length; i += 2) {
result[i] = i / 2;
result[i + 1] = Math.random();
}
return result;
}
Question 3
(6 points)
boolean b = (x > 10 && y > 10) || (x <=10 && y <= 0) ;
Question 4
**Error handling try catch or throws clause expected
(20 points)
import java.util.Scanner;
import java.io.File;
import java.io.IOException;
public class average {
public static void main(String[] args) throws IOException {
File f = new File("score.txt");
Scanner in = new Scanner(f);
double sum = 0.0;
int count = 0;
while(in.hasNext()) {
String name = in.next();
int score = in.nextInt();
sum += score;
count++;
}
if(count > 0)
System.out.println("Average: " + sum / count);
}
}
Gayani Gupta
1
Marking scheme-CS1050_Quiz
Question 5 -
(20 points)
import java.util.Scanner;
public class largeTwo {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int top1 = -1;
int top2 = -1;
int num = in.nextInt();
while(num >= 0) {
if(num > top1) {
top2 = top1;
top1 = num;
}
else if(num > top2)
top2 = num;
num = in.nextInt();
}
if(top1 >= 0 && top2 >= 0)
System.out.println(top1 + ", " + top2);
else if(top1 >= 0)
System.out.println(top1);
}
}
Question 6-***Maximum documentation expected
(30 points)
import java.util.Scanner;
import java.text.DecimalFormat;
/** This program demonstrates parallel arrays. */
public class ParallelArrays{
public static void main(String [] args)
{ final int NUM_EMPLOYEES = 3;
int[] hours = new int[NUM_EMPLOYEES];
double[] payRates = new double[NUM_EMPLOYEES];
// Get the hours worked by each employee.
getPayData(hours, payRates);
// Display each employee's gross pay.
displayGrossPay(hours, payRates);
}
/** The getPayData method accepts as arguments arrays
for employees' hours and pay rates. The user enters
values for these arrays. */
private static void getPayData(int[] hours, double[] payRates)
{ // Create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);
// Get each employee's hours worked and pay rate.
Gayani Gupta
2
Marking scheme-CS1050_Quiz
for (int i = 0; i < hours.length; i++){
// Get the hours worked for this employee.
System.out.print("Enter the hours worked by " +
"employee #" + (i + 1) +": ");
hours[i] = keyboard.nextInt();
// Get the hourly pay rate for this employee.
System.out.print("Enter the hourly pay rate for " +
"employee #" + (i + 1) + ": ");
payRates[i] = keyboard.nextDouble();
}
}
/**
The displayGrossPay method accepts as arguments
arrays for employees' hours and pay rates. The
method uses these arrays to calculate and display
each employee’s gross pay.
*/
private static void displayGrossPay(int [] hours,double [] payRates)
{
double grossPay; // To hold gross pay
// Create a DecimalFormat object.
DecimalFormat dollar = new DecimalFormat("#,##0.00");
// Display each employee's gross pay.67 for (int i = 0; i < hours.length;
i++)
{// Calculate the gross pay.
grossPay = hours[i] * payRates[i];
// Display the gross pay.
System.out.println("The gross pay for " +
"employee #" + (i + 1) +
" is $" +
dollar.format(grossPay));
}
}
}
Gayani Gupta
3
Related documents