Full file at

Similar documents
Chapter 2: Basic Elements of Java

Building Java Programs

CEN 414 Java Programming

Oct Decision Structures cont d

Full file at

Data Types and the while Statement


! definite loop: A loop that executes a known number of times. " The for loops we have seen so far are definite loops. ! We often use language like

Conditional Execution

Chapter 2. Elementary Programming

Chapter 4: Control Structures I

CS 1063 Introduction to Computer Programming Midterm Exam 2 Section 1 Sample Exam

1. An operation in which an overall value is computed incrementally, often using a loop.

Computer Programming, I. Laboratory Manual. Experiment #2. Elementary Programming

Final Examination Semester 3 / Year 2012

Problem Solving With Loops

Exam 2. Programming I (CPCS 202) Instructor: M. G. Abbas Malik. Total Marks: 40 Obtained Marks:

Chapter 2 ELEMENTARY PROGRAMMING

CSE 20. SAMPLE FINAL Version A Time: 180 minutes. The following precedence table is provided for your use:

Chapter 2: Review Exercise Solutions R2.1

Example: Monte Carlo Simulation 1

Java Foundations: Introduction to Program Design & Data Structures, 4e John Lewis, Peter DePasquale, Joseph Chase Test Bank: Chapter 2

Give one example where you might wish to use a three dimensional array

Introduction to Computer Science Unit 2. Notes

Introduction to Computer Science Unit 2. Notes

COSC 123 Computer Creativity. Introduction to Java. Dr. Ramon Lawrence University of British Columbia Okanagan

Arrays. Eng. Mohammed Abdualal

Chapter 2 Elementary Programming

Building Java Programs

Lesson 7 Part 2 Flags

COMP 202. Built in Libraries and objects. CONTENTS: Introduction to objects Introduction to some basic Java libraries string

Section 2.2 Your First Program in Java: Printing a Line of Text

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Last Class. While loops Infinite loops Loop counters Iterations

CSc 2010 Principles of Computer Science, Fall 2013 Practice Problems for Midterm 3* * 3 17 % 9-20 % (26 / 7) "2"

COMP-202 Unit 4: Programming With Iterations. CONTENTS: The while and for statements

Building Java Programs

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Computer Programming, I. Laboratory Manual. Experiment #3. Selections

Controls Structure for Repetition

COMP-202B - Introduction to Computing I (Winter 2011) - All Sections Example Questions for In-Class Quiz

Please answer the following questions. Do not re-code the enclosed codes if you have already completed them.

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

b. Suppose you enter input from the console, when you run the program. What is the output?

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

Arrays. Weather Problem Array Declaration Accessing Elements Arrays and for Loops Array length field Quick Array Initialization Array Traversals

Chapter 3. Selections

Check out how to use the random number generator (introduced in section 4.11 of the text) to get a number between 1 and 6 to create the simulation.

Programming with Java

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Java Basics

Objectives. In this chapter, you will:

NoSuchElementException 5. Name of the Exception that occurs when you try to read past the end of the input data in a file.

Lecture Notes. System.out.println( Circle radius: + radius + area: + area); radius radius area area value

EKT 120/4 Computer Programming KOLEJ UNIVERSITI KEJURUTERAAN UTARA MALAYSIA

Elementary Programming. CSE 114, Computer Science 1 Stony Brook University

Fundamentals of Programming Data Types & Methods

4 WORKING WITH DATA TYPES AND OPERATIONS

Chapter 1 INTRODUCTION

Chapter 02: Using Data

In this chapter, you will:

CS110: PROGRAMMING LANGUAGE I

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

Basic Computation. Chapter 2

Object Oriented Programming. Java-Lecture 1

Elementary Programming

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Jump Statements. The keyword break and continue are often used in repetition structures to provide additional controls.

Activity 1: Introduction

Mid Term Exam 1. Programming I (CPCS 202) Instructor: M. G. Abbas Malik Date: Sunday November 3, 2013 Total Marks: 50 Obtained Marks:

Computer Programming, I. Laboratory Manual. Experiment #6. Loops

Darrell Bethea May 25, 2011

H212 Introduction to Software Systems Honors

Building Java Programs

COMP-202: Foundations of Programming. Lecture 4: Flow Control Loops Sandeep Manjanna, Summer 2015

Chapter 2: Using Data

Introduction to Java Unit 1. Using BlueJ to Write Programs

Algorithms and Problem Solving

Arrays. Here is the generic syntax for an array declaration: type[] <var_name>; Here's an example: int[] numbers;

AP Computer Science Unit 1. Programs

Chapter 1. Introduction to Computers and Programming. M hiwa ahmad aziz

AP Computer Science A Unit 2. Exercises

VARIABLE, OPERATOR AND EXPRESSION [SET 1]

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

AP CS Unit 3: Control Structures Notes

Midterm Examination (MTA)

Date: Dr. Essam Halim

Tester vs. Controller. Elementary Programming. Learning Outcomes. Compile Time vs. Run Time

C++ Programming: From Problem Analysis to Program Design, Third Edition

MIDTERM REVIEW. midterminformation.htm

false, import, new 1 class Lecture2 { 2 3 "Data types, Variables, and Operators" 4

Ch. 6. User-Defined Methods

Computational Expression

CSE 201 JAVA PROGRAMMING I. Copyright 2016 by Smart Coding School

1 class Lecture5 { 2 3 "Methods" / References 8 [1] Ch. 5 in YDL 9 [1] Ch. 20 in YDL 0 / Zheng-Liang Lu Java Programming 176 / 199

Lesson 02 Data Types and Statements. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

1. Which of the following is the correct expression of character 4? a. 4 b. "4" c. '\0004' d. '4'

Outline. Parts 1 to 3 introduce and sketch out the ideas of OOP. Part 5 deals with these ideas in closer detail.

Question: Total Points: Score:

Transcription:

Chapter 1 1. a. False; b. False; c. True; d. False; e. False; f; True; g. True; h. False; i. False; j. True; k. False; l. True. 2. Keyboard and mouse. 3. Monitor and printer. 4. Because programs and data must be stored in main memory before processing and because everything in main memory is lost when the computer is turned off, information stored in main memory must be transferred to some other device for permanent storage. Secondary storage permanently stores programs and data. 5. An operating system monitors the overall activity of the computer and provides services. Some of these services include memory management, input/output activities, and storage management. 6. The two types of programs are system programs and application programs. 7. In machine language the programs are written using the binary codes while in high-level language the program are closer to the natural language. For execution, a high-level language program is translated into the machine language while a machine language need not be translated into any other language. 8. A program written in a high-level language is called a source program. 9. Syntax errors. 10. Every computer directly understands its machine language. Therefore, for the computer to execute a program written in a high level language, the high level language program must be translated into the computer s machine language. 11. Instructions in a high level language are closer to the natural language, such as English, and therefore, are easier to understand and learn than the machine language. 12. A well-analyzed problem leads to a well designed algorithm. Moreover, a program that is wellanalyzed is easier to modify and spot and fix errors. 13. To find the weighted average of the four test scores, first you need to know each test score and its weight. Next, you multiply each test score with its weight, and then add these numbers to get the average. Therefore, 1. Get testscore1, weighttestscore1 2. Get testscore2, weighttestscore2 3. Get testscore3, weighttestscore3 4. Get testscore4, weighttestscore4 5. weightedaverage = testscore1 * weighttestscore1 + testscore2 * weighttestscore2 + testscore3 * weighttestscore3 + testscore4 * weighttestscore4; 14. To find the price per square inch, first we need to find the area of the pizza. Then we divide the price of the pizza by the area of the pizza. Let radius denote the radius and area denote the area of the

circle, and price denote the price of pizza. Also, let pricepersquareinch denote the price per square inch. a. Get radius b. area = π * radius * radius c. Get price d. pricepersquareinch = price / area 15. To calculate the selling price of an item, we need to know the original price (the price the store pays to buy) of the item. We can then the use the following formula to find the selling price: sellingprice = (originalprice + originalprice 0.80) 0.90 The algorithm is as follows: a. Get originalprice b. Calculate the sellingprice using the formula: sellingprice = (originalprice + originalprice 0.80) 0.90 The information needed to calculate the selling price is the original price and the marked-up percentage. 16. To calculate the area of the triangle using the given formula, we need to know the lengths of the sides, a, b, and c, of the triangle. Next we calculate s using the formula s = (1/2)(a + b + c) next calculate the area using the formula: area = sqrt(s(s-a)(s-b)(s-c)) where sqrt denotes the square root. The algorithm is therefore: a. Get a, b, c b. s = (1/2)(a + b + c) c. area = sqrt(s(s-a)(s-b)(s-c)) The information needed to calculate the area of the triangle is the lengths of the sides of the triangle. 17. Suppose that numofpages denotes the number of pages to be faxed and billingamount denotes the total charges for the pages faxed. To calculate the total charges, you need to know the number of pages faxed. If numofpages is less than or equal to ten, the billing amount is service charges plus (numofpages 0.20); otherwise, billing amount is service charges plus 10 0.20 plus (numofpages - 10) 0.10. You can now write the algorithm as follows: a. Get numofpages. b. Calculate billing amount using the formula: if (numofpages is less than or equal to 10) billingamount = 3.00 + (numofpages 0.20);

otherwise billingamount = 3.00 + 10 0.20 + (numofpages - 10) 0.10; 18. Suppose averagetestscore denotes the average test score, highestscore denotes the highest test score, testscore denotes a test score, sum denotes the sum of all the test scores, and count denotes the number of students in class, and studentname denotes the name of a student. a. First you design an algorithm to find the average test score. To find the average test score, first you need to count the number of students in the class and add the test score of each student. You then divide the sum by count to find the average test score. The algorithm to find the average test score is as follows: i. Set sum and count to 0. ii. Repeat the following for each student in class. 1. Get testscore 2. Increment count and update the value of sum by adding the current test score to sum. iii. Use the following formula to find the average test score. if (count is 0) averagetestscore = 0; otherwise averagetestscore = sum / count; b. The following algorithm determines and prints the names of all the students whose test score is below the average test score. Repeat the following for each student in class: i. Get studentname and testscore ii. if (testscore is less than averagetestscore) print studentname c. The following algorithm determines and highest test score i. Get first student s test score and call it highesttestscore. ii. Repeat the following for each of the remaining students in class 1. Get testscore 2. if (testscore is greater than highesttestscore) highesttestscore = testscore; d. To print the names of all the students whose test score is the same as the highest test score, compare the test score of each student with the highest test score and if they are equal print the name. The following algorithm accomplishes this Repeat the following for each student in class: i. Get studentname and testscore ii. if (testscore is equal to highesttestscore)

print studentname You can use the solutions of the subproblems obtained in parts a to d to design the main algorithm as follows: 1. Use the algorithm in part a to find the average test score. 2. Use the algorithm in part b to print the names of all the students whose score is below the average test score. 3. Use the algorithm in part c to find the highest test score. 4. Use the algorithm in part d to print the names of all the students whose test score is the same as the highest test score

Chapter 2 1. a. False; b. False; c. False; d. False; e. True; f. True; g. True; h. False; i. True; j. False; 2. a, c, d, e, j 3. a 4. A keyword is a reserved word and is defined by the system. A keyword cannot be redefined in a program. A user-defined identifier can be redefined. 5. The identifiers firstname and FirstName are not the same. Java is case sensitive. The first character of firstname is lowercase f while the first character of FirstName is uppercase F. So these identifiers are different. 6 a. 8 b. 14 c. 4 d. -2 e. 4.5 f. 25.5 g. 15 h. 16.2 7. a. 3; b. 0.5; c. 4.5; d. 38.5; e. 1; f. 2; g. 2; h. 420.0; 8. a, b, c, e, i, j, and k are valid; d, f, and g are invalid because the left side of an assignment must be a variable not an expression; h. The expression num1 % 2.0 evaluates to a decimal number, while num2 is an int variable. A decimal value cannot be stored in an int variable. 9. 7 10. Only the variable declarations in Lines 3 and 4 are correct 11. a and c are valid 12. a. int x, y; b. x = 10; ch = 'B'; c. x = x + 5; d. double payrate = 12.50; e. tempnum = firstnum; f. int temp = x; x = y; y = temp; g. System.out.println(x + " " + y + " " + (x + 12 / y - 18)); h. char grade = 'A'; i. int num1, num2, num3, num4; j. x = (int)(z + 0.5); 13. a. 32 * a + b b. '8' c. "Julie Nelson" d. (b * b 4 * a * c) / (2 * a)

e. (a + b) / c * (e * f) g * h f. ( b + (b * b 4 * a * c)) / (2 * a) 14. x = 5 y = 2 z = 3 w = 9 15. x = 20 y = 15 z = 6 w = 11.5 t = 4.5 16. a. 92. (Note that x + ' ' + y is evaluate as (x + ' ') + y = (25 + 32) + 35) = 57 + 35 = 92. Moreover, char data type is a subcategory of integral data type and the Unicode value of ' ' is 32.) b. 25 35 17. a. x = 2, y = 5, z = 6 b. x + y = 7 c. Sum of 2 and 6 is 8 d. z / x = 3 e. 2 times 2 = 4 18. a. 0.5 b. 24.5 c. 37.6 d. 8.3 e. 10 f. 38.75 19. a. or System.out.print("\n"); or System.out.print('\n'); b. System.out.println("\t"); c. System.out.println("\""); 20. a and c are correct 21. a. firstname b. discountedprice c. numofjuicebottles d. milestravelled e. highesttestscore 22. a. int num1; int num2; b. System.out.println("Enter two numbers separated by spaces."); c. (Assume that console is a Scanner object initialized to the standard input device.) num1 = console.nextint();

num2 = console.nextint(); d. System.out.println("num1 = " + num1 + "num2 = " + num2 + "2 * num1 num2 = " + (2 * num1 num2)); 23. A correct answer is: public class Exercise23 static final int SECRET_NUM = 11213; static final double PAY_RATE = 18.35; public static void main(string[] arg) int one, two, three; double first, second; double paycheck, hoursworked; one = 18; two = 11; three = 3; first = 25; second = first * three; second = 2 * SECRET_NUM; System.out.println(first + " " + second + " " + SECRET_NUM); hoursworked = 35; paycheck = hoursworked * PAY_RATE; System.out.println("Wages = " + paycheck); 24. A correct answer is. import java.util.*; public class Exercise24 static Scanner console = new Scanner(System.in); public static void main(string[] args) int temperature; String first, last; System.out.print("Enter first name: "); first = console.next(); System.out.print("Enter last name: "); last = console.next();

System.out.print("Enter today s temperature: "); temperature = console.nextint(); System.out.println(first + " " + last + " today s temperature is: " + temperature); 25. A correct answer is: public class Exercise25 static final char STAR = '*'; static final int PRIME = 71; public static void main(string[] arg) int count = 1; int sum = count + PRIME; double x = 25.67; int newnum = count * PRIME + 2; sum = sum + count; x = x + sum * count; System.out.println(" count = " + count + ", sum = " + sum + ", PRIME = " + PRIME); 26. A variable must be declared before it can be used. 27. The class String is contained in the package java.lang. You do not need to import classes from the package java.lang. The system automatically does it for you. 28. a. x *= 2; b. x += y-2; c. sum += num; d. z *= x + 2; e. y /= x + 5; 29. a. x = x + 5 z; 30. b. y = y * (2 * x + 5 z); c. w = w + 2 * z + 4; d. x = x (z + y t); e. sum = sum + num; f. x = x / (y 2); a b c a = (b++) + 3; 9 7 UND c = 2 * a + (++b); 9 8 26 b = 2 * (++c) (a++); 10 45 27

31. a b c sum sum = a + b + (int)c; 3 5 14.1 22 c /= a; 3 5 4.7 22 b += (int)c a; 3 6 4.7 22 a *= 2 * b + (int)c; 48 6 4.7 22 32. (NOTE: The user input us shaded.) a = 25 Enter the first integers: 20 Enter the second integers: 15 The numbers you entered are 20 and 15 z = 45.5 Your grade is A The value of a = 65 33. (NOTE: The user input is shaded.) 34. Enter last name: Miller Enter a two digit number: 34 Enter a positive integer less than 1000: 340 Name: Miller Id: 3417 Mystery number: 3689 import java.util.*; public class Exercise34 static Scanner console = new Scanner(System.in); static final double X = 13.45; static final int Y = 34; static final char BLANK = ' '; public static void main(string[] arg) String firstname; String lastname; int num; double salary; System.out.print("Enter first name: "); firstname = console.next(); System.out.print("Enter last name: "); lastname = console.next(); System.out.print("Enter a positive integer less " + "than 70: "); num = console.nextint();

salary = num * X; System.out.println("Name: " + firstname + BLANK + lastname); System.out.println("Wages: $" + salary); System.out.println("X = " + X); System.out.println("X + Y = " + (X + Y)); 35. The program requires three inputs. One possible form of input is: number string number Another possible form of input is: number string number