cs111firstterm37.files.wordpress.com · web viewprogram requests another test result. 2. count the...

8
CS111: Programming Language II Lab1: Review on Java Basics Computer Science Department Spring 2015

Upload: hoangkhue

Post on 25-Apr-2018

221 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: cs111firstterm37.files.wordpress.com · Web viewprogram requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results, indicating

CS111: ProgrammingLanguage IILab1: Review on Java Basics

Computer Science DepartmentSpring 2015

Page 2: cs111firstterm37.files.wordpress.com · Web viewprogram requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results, indicating

Lab Objectives:

P a g e | 2P a g e | 2

Gives a quick overview on the basics of java language covered previously in CS110

Lab Exercise 1:

Problem Description

A college offers a course that prepares students for the state licensing exam for real estate brokers. Last year, ten of the students who completed this course took the exam. The college wants to know how well its students did on the exam. You’ve been asked to write a program to summarize the results. You’ve been given a list of 10 numbers. 1 means that the student passed the exam and 2 means that the student failed.

Your program should analyze the results of the exam as follows:

1. Input each test result (i.e., a 1 or a 2). Display the message “Enter result” on the screen each time theprogram requests another test result.

2. Count the number of test results of each type.

3. Display a summary of the test results, indicating the number of students who passed and the number who failed.

4. If more than eight students passed the exam, print the message “Bonus to instructor!”

[hint : write your program using a while loop]

Sample output

Page 3: cs111firstterm37.files.wordpress.com · Web viewprogram requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results, indicating

P a g e | 3P a g e | 3

Code Skelton

// Analysis of examination results.

import java.util.Scanner; // class uses class Scanner

public class Analysis{

public static void main( String[] args ){

// create Scanner to obtain input from command window

// declare and initialize needed variables// number of passes// number of failures// student counter// one exam result

// process 10 students using counter-controlled loop while ( ){

// prompt user for input and obtain value from userSystem.out.print( "Enter result (1 = pass, 2 = fail): " );

// test the input value

// increment studentCounter so loop eventually terminates

} // end while

// display resultsSystem.out.printf( "Passed: %d\nFailed: %d\n", passes, failures );

// determine whether more than 8 students passed

} // end main} // end class Analysis

Follow-up Questions and Activities:

1. Rewrite the above program with for loop instead of while.2. What if the number of students is not known before the program runs. How could you change the above program to read a sequence of scores that ends with a -1 as a sentinel value.

Page 4: cs111firstterm37.files.wordpress.com · Web viewprogram requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results, indicating

Lab Exercise 2:

P a g e | 4P a g e | 4

Problem Description

Write a program that reads a number from the user and displays if the number is prime or not prime. Aprime number is a number such that one and itself are the only numbers that evenly divide it (e.g.,3,5,7,11,13,17, …).

Sample output

Please Enter a number >> 1010 is not prime

Please Enter a number >> 77 is prime

Code Skelton

// Prime numbers

import java.util.Scanner; // class uses class Scanner

public class Prime{

public static void main( String[] args ){//define a Boolean variable to hold the result

//declare a variable to hold the input value

// create Scanner to obtain input from command window

// prompt user to input and obtain value from user

// Assume the number is prime

// Inner loop tests whether or not n is prime for (i=2; i<= n-1; i++){// number is not prime if number/i has no remainder

}

// Check if flag value is still true// Output that number is prime// otherwise print the number is not prime

}//end of main}//end of class

Page 5: cs111firstterm37.files.wordpress.com · Web viewprogram requests another test result. 2. Count the number of test results of each type. 3. Display a summary of the test results, indicating

P a g e | 5P a g e | 5

Assignment Problem:Write a method isEven that uses the remainder operator (%) to determine whether an integer is even. The method should take an integer argument and return true if the integer is even and false otherwise.

Incorporate this method into an application that inputs a sequence of integers (one at a time) and determines whether each is even or odd. Use the sentinel value -9999 to stop.

SAMPLE RUN: