Chapter 2 Exercise Solutions

Similar documents
Chapter 2: Objects and Primitive Data

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 9, Name: KEY

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 7, Name:

CSC 1051 Data Structures and Algorithms I. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

CSC 1051 Algorithms and Data Structures I. Midterm Examination March 2, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 24, Name: KEY 1

Data Representation and Applets

Data Representation and Applets

Data Representation and Applets

Applets and the Graphics class


CSC Algorithms and Data Structures I. Midterm Examination February 25, Name:

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 25, Name: KEY A

CSC 1051 Algorithms and Data Structures I. Midterm Examination February 26, Name: Key

1. Escape Sequences For each of the following snippets of code write down what will be printed on the screen.

Chapter 3 Syntax, Errors, and Debugging. Fundamentals of Java

Table of Contents Date(s) Title/Topic Page #s. Abstraction

First Java Program - Output to the Screen

Chapter 3 Intro to Java

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Learning objectives: Objects and Primitive Data. Introduction to Objects. A Predefined Object. The print versus the println Methods

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

TWO-DIMENSIONAL FIGURES

Professor: Sana Odeh Lecture 3 Python 3.1 Variables, Primitive Data Types & arithmetic operators

We will start our journey into Processing with creating static images using commands available in Processing:

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Introduction to Java Applications

Chapter 2: Data and Expressions

Program Fundamentals

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

Lecture 3: Variables and assignment

Chapter. Let's explore some other fundamental programming concepts

Objects and Primitive Data

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Chapter 2: Data and Expressions

Data Representation and Applets

Operators DECREASE in strength toward the Bottom

We now start exploring some key elements of the Java programming language and ways of performing I/O

Entry Point of Execution: the main Method. Elementary Programming. Learning Outcomes. Development Process

Appendix F: Java Graphics

Repe$$on CSC 121 Spring 2017 Howard Rosenthal

A variable is a name for a location in memory A variable must be declared

Lab 3: Work with data (IV)

Lecture Set 2: Starting Java

Appendix F: Java Graphics

Lecture Set 2: Starting Java

Software Concepts. 2-8 Explain the process of translating and executing a Java program. How might the World Wide Web be involved?

COMP 202 Java in one week

Class 9: Static Methods and Data Members

Introduction to Software Development (ISD) David Weston and Igor Razgon

4 Programming Fundamentals. Introduction to Programming 1 1

4 WORKING WITH DATA TYPES AND OPERATIONS

Chapter 2: Using Data

Full file at

Programs, Statements, Variables

CSC 1051 Algorithms and Data Structures I. Midterm Examination October 6, Name:

Computational Expression

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2017 Howard Rosenthal

Building Java Programs. Chapter 2: Primitive Data and Definite Loops

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

Chapter 2: Programming Concepts

TUGCE KEMEROZ - ASLI OZKAN - AYSE TARTAN. Week 12/02/ /02/2007 Lecture Notes:

Elementary Programming

Chapter 2: Review Exercise Solutions R2.1

Chapter 2: Data and Expressions. Prelab Exercises. Print and println. Area and Circumference of a Circle. Integers and Floating point

Primitive Data, Variables, and Expressions; Simple Conditional Execution

Review. Primitive Data Types & Variables. String Mathematical operators: + - * / % Comparison: < > <= >= == int, long float, double boolean char

Preparation: Get to know the Java coordinate system. 3. How is the background color of the window specified? Copy the exact code:

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Chapter 1 Introduction to java

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

Practice Midterm 1. Problem Points Score TOTAL 50

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Practice Midterm 1 Answer Key

Conditionals. For exercises 1 to 27, indicate the output that will be produced. Assume the following declarations:

12/22/11. Java How to Program, 9/e. Help you get started with Eclipse and NetBeans integrated development environments.

Repe$$on CSC 121 Fall 2015 Howard Rosenthal

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

Variables One More (but not the last) Time with feeling

CS 106 Introduction to Computer Science I

Course Outline. Introduction to java

Basic Computation. Chapter 2

B.V. Patel Institute of BMC & IT, UTU 2014

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

CS111: PROGRAMMING LANGUAGE II

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Lecture 6. Assignments. Summary - Variables. Summary Program Parts 1/29/18. Reading: 3.1, 3.2, 3.3, 3.4

AP Computer Science Unit 1. Programs

Chapter 2: Data and Expressions

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

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

Programming Exercise 7: Static Methods

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

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

Final Exam. COMP Summer I June 26, points

Chapter 2: Basic Elements of Java

Transcription:

Chapter 2 Exercise Solutions EX 2.1. EX 2.2. EX 2.3. EX 2.4. EX 2.5. Explain the following programming statement in terms of objects and the services they provide. System.out.println ("I gotta be me!"); The System.out object has a println method which accepts a string, enclosed in parentheses and quotation marks, which it displays on the monitor. What output is produced by the following code fragment? Explain. System.out.print ("Here we go!"); System.out.println ("12345"); System.out.print ("Test this if you are not sure."); System.out.print ("Another."); System.out.println (); System.out.println ("All done."); Here we go!12345 Test this if you are not sure.another. All done. After printing its data, the println method moves to the next line of output, whereas the print method does not. A println statement with no data has the effect of moving down to the next line. What is wrong with the following program statement? How can it be fixed? System.out.println ("To be or not to be, that is the question."); The string to be printed is not all on one line. The problem can be fixed by using the string concatenation operator (+) or by using a print statement for part of the string and a println statement for the remainder of the string. What output is produced by the following statement? Explain. System.out.println ("50 plus 25 is " + 50 + 25); 50 plus 25 is 5025 First the string 50 plus 25 is concatenated with the integer 50; since one of the operands associated with the + operator is a string, the result is a string. Then the string 50 plus 25 is 50 is concatenated with the integer 25, with similar results. What is the output produced by the following statement? Explain. System.out.println ("He thrusts his fists\n\tagainst" + " the post\nand still insists\n\the sees the \"ghost\""); He thrusts his fists against the post and still insists he sees the "ghost"

Escape characters are used to go to the beginning of new lines (\n), to tab (\t), and to print quotation marks (\"). EX 2.6. EX 2.7. EX 2.8. EX 2.9. What value is contained in the integer variable size after the following size = 18; size = size + 12; size = size * 2; size = size / 4; The final value stored in size is 15. What value is contained in the floating point variable depth after the following depth = 2.4; depth = 20 depth * 4; depth = depth / 5; The final value stored in depth is 2.08. What value is contained in the integer variable length after the following length = 5; length *= 2; length *= length; length /= 100; The final value stored in length is 1. Write four different program statements that increment the value of an integer variable total. total = total + 1; total += 1; total++; ++total; EX 2.10. Given the following declarations, what result is stored in each of the listed assignment statements? int iresult, num1 = 25, num2 = 40, num3 = 17, num4 = 5; double fresult, val1 = 17.0, val2 = 12.78; a. iresult = num1 / num4; iresult is assigned 5 b. fresult = num1 / num4; fresult is assigned 5.0 c. iresult = num3 / num4; iresult is assigned 3 d. fresult = num3 / num4;

fresult is assigned 3.0 e. fresult = val1 / num4; fresult is assigned 3.4 f. fresult = val1 / val2; fresult is assigned 1.3302034... g. iresult = num1 / num2; iresult is assigned 0 h. fresult = (double) num1 / num2; fresult is assigned 0.625 i. fresult = num1 / (double) num2; fresult is assigned 0.625 j. fresult = (double) (num1 / num2); fresult is assigned 0.0 k. iresult = (int) (val1 / num4); iresult is assigned 3 l. fresult = (int) (val1 / num4); fresult is assigned 3.0 m. fresult = (int) ((double) num1 / num2); fresult is assigned 0.0 n. iresult = num3 % num4; iresult is assigned 2 o. iresult = num 2 % num3; iresult is assigned 6 p. iresult = num3 % num2; iresult is assigned 17 q. iresult = num2 % num4; iresult is assigned 0 EX 2.11. For each of the following expressions, indicate the order in which the operators will be evaluated by writing a number beneath each operator. a. a b c d b. a b + c d c. a + b / c / d d. a + b / c * d

e. a / b * c * d f. a % b / c * d g. a % b % c % d h. a (b c) d 2 1 3 i. (a (b c)) d 2 1 3 j. a ((b c) d) k. a % (b % c) * d * e 2 1 3 4 l. a + (b c) * d e 4 m. (a + b) * c + d * e 1 2 4 3 n. (a + b) * (c / d) % e 1 3 2 4 EX 2.12. Explain the role played by the World Wide Web in the translation and execution of some Java programs. Java applets are designed to be moved across the web and executed in a browser. The applet source code is compiled into Java bytecode which is sent across the Internet using HMTL and the World Wide Web software. A Web browser with a Java interpreter interprets and execute the bytecode. EX 2.13. Compare and contrast a traditional coordinate system and the coordinate system used by Java graphical components. A traditional coordinate system has the origin in the lower-left corner, with x increasing to the right and y increasing upward. The coordinate system used by Java has the origin in the upper-left corner with x increasing to the right and y increasing downward. EX 2.14. How many bits are needed to store a color picture that is 400 pixels wide and 250 pixels high? Assume color is represented using the RGB technique described in this chapter and that no special compression is done. Allowing 8 bits for each of the 3 (red, green, and blue) color components of each of 400 x 250 pixels comprising the picture means that 8 x 3 x 400 x 250 = 2,400,000 bits are needed. EX 2.15. Assuming you have a Graphics object called page, write a statement that will draw a line from point (20, 30) to point (50, 60). page.drawline(20,30,50,60);

EX 2.16. Assuming you have a Graphics object called page, write a statement that will draw a rectangle with height 70 and width 35, such that its upper left corner is at point (10, 15). page.drawrect(10,15,35,70); EX 2.17. Assuming you have a Graphics object called page, write a statement that will draw a circle centered on point (50, 50) with a radius of 20 pixels. page.drawoval(30,30,40,40); EX 2.18. The following lines of code draw the eyes of the snowman in the Snowman applet. The eyes seem centered on the face when drawn, but the first parameters of each call are not equally offset from the midpoint. Explain. page.filloval (MID-10, TOP+10, 5, 5); page.filloval (MID+5, TOP+10, 5, 5); The first parameter specifies the upperleft corner of the eyes, not the center of the eyes. The eyes are circles of diameter 5. Hence, the center of the first eye is 10 + 2.5 = -7.5 units to the left of MID, and the second eye is 5 + 2.5 = 7.5 units to the right of MID.