MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION



Similar documents
System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Sample CSE8A midterm Multiple Choice (circle one)

Basics of Java Programming Input and the Scanner class

Introduction to Java

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

JAVA ARRAY EXAMPLE PDF

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Chapter 2 Introduction to Java programming

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

AP Computer Science Static Methods, Strings, User Input

AP Computer Science Java Mr. Clausen Program 9A, 9B

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

Part I. Multiple Choice Questions (2 points each):

Topic 11 Scanner object, conditional execution

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Using Files as Input/Output in Java 5.0 Applications

Arrays. Introduction. Chapter 7

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Programming

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

In this Chapter you ll learn:

Homework/Program #5 Solutions

Iteration CHAPTER 6. Topic Summary

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

The following program is aiming to extract from a simple text file an analysis of the content such as:

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Building Java Programs

Building Java Programs

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

(Eng. Hayam Reda Seireg) Sheet Java

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Chapter 2 Elementary Programming

Course Intro Instructor Intro Java Intro, Continued

Two-Dimensional Arrays. Multi-dimensional Arrays. Two-Dimensional Array Indexing

CS114: Introduction to Java

CS 112/ Section 02 Hilal Ünal Aslıhan Ekim Merve Özkılınç Notes of March 11, 2008 and March 13, 2008: Pig Latin: add adday. main.

J a v a Quiz (Unit 3, Test 0 Practice)

LOOPS CHAPTER CHAPTER GOALS

Chapter 2: Elements of Java

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

Lecture 1 Introduction to Java

Building Java Programs

3 length + 23 size = 2

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

While and Do-While Loops Summer 2010 Margaret Reid-Miller

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

Building Java Programs

Week 1: Review of Java Programming Basics

1. Use the class definition above to circle and identify the parts of code from the list given in parts a j.

CS 121 Intro to Programming:Java - Lecture 11 Announcements

Arrays in Java. Working with Arrays

Comp 248 Introduction to Programming

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

AP Computer Science File Input with Scanner

Programming Fundamentals I CS 110, Central Washington University. November 2015

Data Structures Lecture 1

Using Two-Dimensional Arrays

Assignment No.3. /*-- Program for addition of two numbers using C++ --*/

AP Computer Science Java Subset

Programming and Data Structures with Java and JUnit. Rick Mercer

Install Java Development Kit (JDK) 1.8

Pseudo code Tutorial and Exercises Teacher s Version

Unit 6. Loop statements

Class : MAC 286. Data Structure. Research Paper on Sorting Algorithms

Example of a Java program

Crash Course in Java

Object-Oriented Programming in Java

CS 106 Introduction to Computer Science I

6.1. Example: A Tip Calculator 6-1

JAVA - METHODS. Method definition consists of a method header and a method body. The same is shown below:

D06 PROGRAMMING with JAVA

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Chapter 3. Input and output. 3.1 The System class

Software Testing. Jeffrey Carver University of Alabama. April 7, 2016

Moving from CS 61A Scheme to CS 61B Java

CSE 8B Midterm Fall 2015

Chapter 2 Basics of Scanning and Conventional Programming in Java

Java Basics: Data Types, Variables, and Loops

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Line-based file processing

Programming in Java Course Technology, a part of Cengage Learning.

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

13 File Output and Input

More on Objects and Classes

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Grundlæggende Programmering IT-C, Forår Written exam in Introductory Programming

Interactive Applications (CLI) and Math

D06 PROGRAMMING with JAVA

Chapter 7: Sequential Data Structures

CSE 1020 Introduction to Computer Science I A sample nal exam

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Transcription:

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION 1. Write a loop that computes (No need to write a complete program) 100 1 99 2 98 3 97... 4 3 98 2 99 1 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 { 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 { 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 { 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 number 0 occurred 1 time(s) The number 2 occurred 2 time(s) The number 3 occurred 1 time(s) The number 4 occurred 1 time(s) The number 5 occurred 2 time(s) The number 6 occurred 1 time(s) The number 23 occurred 1 time(s) The number 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 { //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: 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 { //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{ //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]);