All classes in a package can be imported by using only one import statement. If the postcondition of a method is not met, blame its implementer

Similar documents
GROUP - A <10 questions x 4 points each = 40 points >

Java By Abstraction - Test-B (Chapters 1-6)

Zheng-Liang Lu Java Programming 45 / 79

CIS133J. Working with Numbers in Java

Chapter 5 Control Structures

1 class Lecture2 { 2 3 "Elementray Programming" / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch.

McGill University School of Computer Science COMP-202A Introduction to Computing 1

A Foundation for Programming

Introduction to Programming Using Java (98-388)

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

A Java program contains at least one class definition.

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

Using APIs. Chapter 3. Outline Fields Overall Layout. Java By Abstraction Chapter 3. Field Summary static double PI

Functions. x y z. f (x, y, z) Take in input arguments (zero or more) Perform some computation - May have side-effects (such as drawing)

Declaration and Memory

Review for Test 1 (Chapter 1-5)

CS 11 java track: lecture 1

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

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

Graphical Interface and Application (I3305) Semester: 1 Academic Year: 2017/2018 Dr Antoun Yaacoub

Chapter 9 Inheritance

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

Lab5. Wooseok Kim

Object Oriented Methods : Deeper Look Lecture Three

1 Shyam sir JAVA Notes

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

Chapter 2: Using Data

Pace University. Fundamental Concepts of CS121 1

Character Stream : It provides a convenient means for handling input and output of characters.

Full file at

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

MACS 261J Final Exam. Question: Total Points: Score:

Lecture 5: Methods CS2301

Chapter 2: Using Data

Java Identifiers, Data Types & Variables

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSC 1214: Object-Oriented Programming

JAVA OPERATORS GENERAL

CS 61B Data Structures and Programming Methodology. June David Sun

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

COS 126 Midterm 1 Written Exam Fall 2011

Selected Questions from by Nageshwara Rao

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Simple Java Reference

York University Fall 2001 / Test #1 Department of Computer Science

Computer Programming C++ (wg) CCOs

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

Chapter 1 Getting Started

Elementary Programming

The Math Class. Using various math class methods. Formatting the values.

Chapter 2. Elementary Programming

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

CIS 110 Introduction To Computer Programming. February 29, 2012 Midterm

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

COP 3330 Final Exam Review

Chapter 02: Using Data

COSC The if Statement. Yves Lespérance. Lecture Notes Week 5 Control Structures

Computer Programming, I. Laboratory Manual. Final Exam Solution

CS11 Java. Fall Lecture 1

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

CT 229 Java Syntax Continued

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

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

To define methods, invoke methods, and pass arguments to a method ( ). To develop reusable code that is modular, easy-toread, easy-to-debug,

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

PROGRAMMING FUNDAMENTALS

Chapter 3: Operators, Expressions and Type Conversion

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

CS 61C: Great Ideas in Computer Architecture Introduction to C

Lecture #6-7 Methods

I pledge by honor that I will not discuss this exam with anyone until my instructor reviews the exam in the class.

Datatypes, Variables, and Operations

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Methods: A Deeper Look

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

EECS1710. Announcements. Labtests have been returned Term Test #01 marking is in progress. Next Labtest: Thu Oct 23/Fri Oct 24

Midterm Examination (MTA)

DATA TYPES AND EXPRESSIONS

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

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

Chapter 2: Data and Expressions

CS110: PROGRAMMING LANGUAGE I

Getting started with Java

Method Resolution Approaches. Dynamic Dispatch

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Topics. The Development Process

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Data Types. 1 You cannot change the type of the variable after declaration. Zheng-Liang Lu Java Programming 52 / 87

C212 Early Evaluation Exam Mon Feb Name: Please provide brief (common sense) justifications with your answers below.

int a; int b = 3; for (a = 0; a < 8 b < 20; a++) {a = a + b; b = b + a;}

Midterm I - CSE11 Fall 2013 CLOSED BOOK, CLOSED NOTES 50 minutes, 100 points Total.

Basic Operations jgrasp debugger Writing Programs & Checkstyle

A very simple program. Week 2: variables & expressions. Declaring variables. Assignments: examples. Initialising variables. Assignments: pattern

Lecture 9: Arrays. Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp. Copyright (c) Pearson All rights reserved.

Chapter 2 Elementary Programming

Methods CSC 121 Fall 2014 Howard Rosenthal

Transcription:

Java By Abstraction ANSWERS O ES-A GROUP - A <10 points > For each question, give +0.5 if correct, -0.5 if wrong, and 0 if blank. If the overall total is negative, record it (on the test's cover sheet) as zero. Parameters in Java are passed by value A method can return at most one thing A package can contain more than one class All classes in a package can be imported by using only one import statement F If a variable is used before being declared, MM will generate an error F he program that executes bytecode is called javac public is a reserved word in Java wo methods in a class can have the same name if they have different signatures F wo methods in a class can have the same signature if they have different return types F wo methods in a class are said to be overloaded if they have the same signature F wo fields in a class can have the same name if they have different types F he expression a+b*c is the same as (a+b)*c he expression a/b*c is the same as (a/b)*c he expression a-a+b-b == 0 is always true F An escape sequence is used to override automatic promotion. F Java is case-insensitive F If x is an int and the expression x/2 evaluated to an integer, then x must be even If x is an int and the expression x%2 evaluated to 1, then x must be odd If the postcondition of a method is not met, blame its implementer F he compiler detects all logic errors in an app Java By Abstraction Answers to est-a Pg. 1 of 10

GROUP - B <10 questions x 2 points each = 20 points > Give 0 or 1 for each output. In case of error, give +1 for indicating the error type and +1 for a correct description of the error. B.1 int a = 3; int b = 28; a = a - 6 % 3; b = b / 5; IO.println(a); IO.println(b); 3 5 B.2 int x = 20; final int YR = 5; x = x % 3; YR = YR / 2; IO.println(x); IO.println(YR); Syntax error in 4th line: attempting to modify a final. B.3 int a = 30; boolean m = "true"; IO.println(a + 1); IO.println(m); Syntax error in the 2nd line: LHS is a boolean, RHS is a String Java By Abstraction Answers to est-a Pg. 2 of 10

GROUP - B, continued B.4 byte a = 120; boolean g = a!= 0; byte b = (byte) (a + 9); IO.println(g); IO.println(b); true -127 B.5 short a = 530; short b = a; IO.println(a + 1); IO.println(b - 1); 531 529 B.6 byte x = 12; char y = 'x'; IO.println(x - 1); IO.println(y - 1); 11 119 (It is not 'w' because the -1 will auto-promote to int. o get 'w', cast to char) B.7 int x = -5; double y = -5 / 2 * 2; IO.println(x % 2); IO.println(y); -1-4. Java By Abstraction Answers to est-a Pg. 3 of 10

GROUP - B, continued B.8 double base = 5.5; double height = 4.0; double area = (1/2) * base * height; double hypo = Math.pow(height, 2); IO.println(area); IO.println(hypo); 0. 16.0 B.9 float mass = 0.5; float weight = 9.8 * mass; IO.println(mass / 2); IO.println(weight); Syntax error in the 2nd line: attempting to assign a double to a float. B.10 double x = 11.2; IO.println(((int) x) / 2); IO.println(Math.rint(x) / 2); 5 5.5 Java By Abstraction Answers to est-a Pg. 4 of 10

GROUP - C <30 points > Consider the following API: type.jba Class Sint his class offers services for manipulating sints. A sint is a special kind of integer; it is a fictitious term that has been made up solely for this test. Field Summary static int MAXI he highest allowed value for a sint Method Summary static double average(int a, int b) Returns the weighted average of the passed sints. static double average(long a, long b) Returns the biased average of the passed sints. he following app, UseSint, uses the Sint class: 1 import type.lang.*; 2 import Sint.*; 3 4 public class UseSint 5 { public static void main(string[] args) 6 { //--------------------------Prompt, Read, and Validate 7 IO.println("Enter the first sint value:"); 8 int h = IO.readInt(); 9 SE.require(h < MAXI); 10 //--------------------------Prompt, Read, and Validate 11 IO.println("Enter the second sint value:"); 12 int k = IO.readInt(); 13 boolean ok = k <= MAXI; 14 //--------------------------Compute averages as per API 15 double w = average(h, k); 16 double b = (long) (average(h, k)); 17 //--------------------------Output averages 18 IO.print("heir weighted average is: "); 19 IO.println(w); 20 IO.print("And their biased average is: "); 21 IO.println(b); 22 23 Java By Abstraction Answers to est-a Pg. 5 of 10

C.1 <10 points > Give +1 for indicating the error type and +1 for a correct description of the error. Any five of the following: Line ype Description 2 Syntax Must specify a package: type.jba.* or type.jba.sint 9 Syntax MAXI is referenced as if it is a local variable! 9 Logical he relational operator < should be <= 13 Syntax MAXI is referenced as if it is a local variable! 13 Logical he second input, k, was not validated! 15 Syntax Cannot invoke average without the dot operator! 16 Syntax Cannot invoke average without the dot operator! 16 Syntax Casting the return does not change the invoked method: the weighted average is computed, not the biased one. Java By Abstraction Answers to est-a Pg. 6 of 10

C.2 <20 points > Rewrite the UseSint app. It is supposed to prompt for and read two sint values from the user, validates them, and then output their weighted and biased averages. 1 import type.lang.*; 2 import type.jba.*; 3 4 public class UseSint 5 { public static void main(string[] args) 6 { //--------------------------Prompt, Read, and Validate 7 IO.println("Enter the first sint value:"); 8 int h = IO.readInt(); 9 SE.require(h <= Sint.MAXI); 10 //--------------------------Prompt, Read, and Validate 11 IO.println("Enter the second sint value:"); 12 int k = IO.readInt(); 13 boolean ok = k <= Sint.MAXI; SE.require(ok); 14 //--------------------------Compute averages as per API 15 double w = Sint.average(h, k); 16 double b = Sint.average((long) h, k)); 17 //--------------------------Output averages 18 IO.print("heir weighted average is: "); 19 IO.println(w); 20 IO.print("And their biased average is: "); 21 IO.println(b); 22 23 Note that in line 16 you can cast either or both parameters to long in order to force a binding with the biased average method. Give 20, 15, 10, 5, or 0. A few minor errors lead to 15, one major error leads to 10, and two or three major errors lead to 5. Java By Abstraction Answers to est-a Pg. 7 of 10

GROUP - D <40 points> D.1 <10 points > Write an app that prompts for and reads an int from the user. If the input is not positive, the app terminates with the error message "Invalid Input". Otherwise, the app must round up the input to the nearest multiple of 25 and output the result. For example, if the input were: 10, 20, 25, 30, 60, 90, or 100 then the corresponding output would be (respectively): 25, 25, 25, 50, 75, 100, or 100 Note that if the input were already a multiple of 25, the output would be equal to it. Note also that even if your algorithm is incorrect, you will still receive partial marks for the rest of the app. import type.lang.*; public class App { public static void main(string[] args) { IO.print("Enter the integer... "); int entry = IO.readInt(); SE.require(entry > 0, "Invalid Input"); int rounded = (entry + 24) / 25 * 25; IO.println(rounded); Give 10, 7, 5, 3, or 0. A few minor errors lead to 7, one major error leads to 5, and two or three major errors lead to 3. Java By Abstraction Answers to est-a Pg. 8 of 10

D.2 <10 points > Write an app that prompts for and reads a double from the user. If the input is not in (0,1000] (i.e. greater than 0 and less than or equal to 1000), the app terminates with the error message "Invalid Input". Otherwise, it implements the following algorithm: Look only at the first digit to the right of the decimal point If that digit is 2 or less, drop all decimal digits If that digit is 3 or higher, drop all decimal digits and increment the integer part by 1 Output the integer part as an int For example, if the input were 15.245, the above algorithm outputs 15, whereas if the input were 15,345, the output would be 16. Note that we drop the decimal part in both cases but the integer part is incremented only if the first decimal is 3 or more. Note that even if your algorithm is incorrect, you will still receive partial marks for the rest of the app. import type.lang.*; public class App { public static void main(string[] args) { IO.print("Enter the double... "); double entry = IO.readDouble(); SE.require(entry > 0, "Invalid Input"); SE.require(entry <= 1000, "Invalid Input"); int rounded = (int) (entry + 0.7); IO.println(rounded); Give 10, 7, 5, 3, or 0. A few minor errors lead to 7, one major error leads to 5, and two or three major errors lead to 3. Java By Abstraction Answers to est-a Pg. 9 of 10

D.3 <20 points > Write an app that reads the x-y coordinates of the centre of a circle and its radius. It then outputs true if the circle passes through the origin, and outputs false if it doesn't. In other words, the output is a boolean. Here is the algorithm of the sought app: Reads x, y, and r from the user (all three are double's). he centre is at (x,y) and the radius is r. Compute the distance d between the centre and the origin. his distance is given by the square root of: x 2 + y 2. In math, where precision is infinite, the circle passes through the origin if d and r are equal. In computing, round off errors are inevitable so we modify this step as follows: If the absolute value of the difference between d and r is less than some very small number EPSILON, we say that the circle passes through the origin; otherwise, we say that it doesn't. For this problem, we take EPSILON to be 1.E-10. import type.lang.*; public class App { public static void main(string[] args) { final double EPSILON = 1.e-10; IO.print("Enter the centre's x... "); double x = IO.readDouble(); IO.print("Enter the centre's y... "); double y = IO.readDouble(); IO.print("Enter the radius... "); double r = IO.readDouble(); double d = Math.pow(x*x + y*y, 0.5); boolean passes = Math.abs(d - r) < EPSILON; IO.println(passes); Give 20, 15, 10, 5, or 0. A few minor errors lead to 15, one major error leads to 10, and two or three major errors lead to 5. Java By Abstraction Answers to est-a Pg. 10 of 10