Tutorial 03. Exercise 1: CSC111 Computer Programming I

Similar documents
Chapter 2. Elementary Programming

AL GHURAIR UNIVERSITY College of Computing. Objectives: Examples: Text-printing program. CSC 209 JAVA I

CEN 414 Java Programming

Lab Exercise 1. Objectives: Part 1. Introduction

Midterm Examination (MTA)

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming

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

Fundamentals of Programming Data Types & Methods

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

Tutorial # 4. Q1. Evaluate the logical (Boolean) expression in the following exercise

AP Computer Science Unit 1. Programs

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Welcome to the Primitives and Expressions Lab!

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

H212 Introduction to Software Systems Honors

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

PRIMITIVE VARIABLES. CS302 Introduction to Programming University of Wisconsin Madison Lecture 3. By Matthew Bernstein

Lecture 8 " INPUT " Instructor: Craig Duckett

Elementary Programming

JAVA OPERATORS GENERAL

AP COMPUTER SCIENCE A

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

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

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

Question 1: (5 marks) Give the output of the following program if the input from the keyboard is:

Full file at

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Simple Control Flow: if-else statements

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

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

King Saud University College of Computer and Information Sciences Computer Science Department

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

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

JAVA Ch. 4. Variables and Constants Lawrenceville Press

Lesson 7 Part 2 Flags

1. [3 pts] What is your section number, the period your discussion meets, and the name of your discussion leader?

Object Oriented Programming. Java-Lecture 1

CSC 1051 Villanova University. CSC 1051 Data Structures and Algorithms I. Course website:

Programming with Java

Java Classes: Math, Integer A C S L E C T U R E 8

Algorithms in everyday life. Algorithms. Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Loops. CSE 114, Computer Science 1 Stony Brook University

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

Conditional Programming

CSC 1051 Data Structures and Algorithms I

Object Oriented Programming. Java-Lecture 6 - Arrays

Motivations 9/14/2010. Introducing Programming with an Example. Chapter 2 Elementary Programming. Objectives

CSC 1051 Data Structures and Algorithms I

International Journal of Advanced Research in Computer Science and Software Engineering

CMSC 150 INTRODUCTION TO COMPUTING LAB WEEK 3 STANDARD IO FORMATTING OUTPUT SCANNER REDIRECTING

PROGRAMMING FUNDAMENTALS

Tutorial 06. Conditional statement: if then, if else, switch

Arrays. Eng. Mohammed Abdualal

Chapter 2. Elementary Programming. Program Listings

CS 106 Introduction to Computer Science I

Entry Point of Execution: the main Method. Elementary Programming. Compile Time vs. Run Time. Learning Outcomes

Introduction to Computer Science Unit 2. Exercises

This exam is open book. Each question is worth 3 points.

Chapter 2 ELEMENTARY PROGRAMMING

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

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Chapter 3 Lab Decision Structures

Motivating Examples (1.1) Selections. Motivating Examples (1.2) Learning Outcomes. EECS1022: Programming for Mobile Computing Winter 2018

Chapter 3 Selections. Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved.

Building Java Programs

CSC 1051 Data Structures and Algorithms I

Algorithms and Conditionals

COSC 243. Data Representation 3. Lecture 3 - Data Representation 3 1. COSC 243 (Computer Architecture)

Algorithms and Java basics: pseudocode, variables, assignment, and interactive programs

Chapter 3: Classes and Objects. CSC2010 Lab 9. Java Programming. Copyright 2000 W. W. Norton & Company. All rights reserved. FROM THE BEGINNING

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

Selections. EECS1021: Object Oriented Programming: from Sensors to Actuators Winter 2019 CHEN-WEI WANG

CS 101 Spring 2007 Midterm 2 Name: ID:

Chapter 2 Elementary Programming. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

Introduction to Computer Science Unit 2. Notes

JAVA Programming Concepts

Oct Decision Structures cont d

Example: Monte Carlo Simulation 1

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Methods

Reading Input from Text File

Beginning Programming (Java) Test 2/Version 1 CMSC 1513 Spring 2013

Motivations. Chapter 2: Elementary Programming 8/24/18. Introducing Programming with an Example. Trace a Program Execution. Trace a Program Execution

Supplementary Test 1

Task #1 The if Statement, Comparing Strings, and Flags

while (/* array size less than 1*/){ System.out.print("Number of students is invalid. Enter" + "number of students: "); /* read array size again */

Nested Loops. A loop can be nested inside another loop.

CSC 231 DYNAMIC PROGRAMMING HOMEWORK Find the optimal order, and its optimal cost, for evaluating the products A 1 A 2 A 3 A 4

1. A company has vans to transport goods from factory to various shops. These vans fall into two categories: 50 items 150 items

Chapter 2: Review Exercise Solutions R2.1

AP Computer Science Unit 1. Writing Programs Using BlueJ

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

Java I/O and Control Structures

New York University Introduction to Computer Science Exam Sample Problems 2013 Andrew I. Case. Instructions:

CS141 Programming Assignment #5

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

Tutorial about Arrays

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

Java Simple Data Types

Building Java Programs

5) (4 points) What is the value of the boolean variable equals after the following statement?

Transcription:

College of Computer and Information Sciences CSC111 Computer Programming I Exercise 1: Tutorial 03 Input & Output Operators Expressions A. Show the result of the following code: 1.System.out.println(2 * (5 / 2 + 5 / 2)); 2.System.out.println(2 * 5 / 2 + 2 * 5 / 2); 3.System.out.println(2 * (5 / 2)); 4.System.out.println(2 * 5 / 2); B. Are the following statements correct? If so, show the output. 1. System.out.println("25 / 4 is " + 25 / 4); 2. System.out.println("25 / 4.0 is " + 25 / 4.0); 3. System.out.println("3 + 2 is " + 3 + 2); 4. System.out.println("3 + 2 is " + (3 + 2)); 5. System.out.println("3 * 2 / 4 is " + 3 * 2 / 4); 6. System.out.println("3.0 * 2 / 4 is " + 3.0 * 2 / 4); C. Suppose m and r are integers. Write a Java expression for mr2 to obtain a floating point result. D. How would you write the following arithmetic expression in Java? 1. 4 3(r+34) 9 (a c) 3+d(2+a) a+bd 2. 2.5+t 5.5 (r + 2.5) E. Show the output of the following code: double a = 6.5; a += a + 1; a = 6; a /= 2; F. Show the output of the following code: int a = 6; int b = a++; System.out.println(b); a = 6; b = ++a; System.out.println(b); G. Is the following code going to print 0.1 and why? System.out.println(1.0 0.9);

Exercise 2: Choose the correct answer: A. The value of 15 / 2 is: (i) (ii).5 (iii) ½ (iv) 0.5 (v) none B. The value of 18 / 3 is: (i) 6 (ii) 0.16 (iii) 6.0(iv) none C. The value of 22 % is: (i) 3 (ii) 1 (iii) 3.142 (iv) 22/ D. The value of 5 % is: (i) 0 (ii) 2 (iii) 5 (iv) undefined E. The value of 1.0 / 4 is: (i) 4 (ii) 4.25 (iii) 4.0(iv) undefined F. The value of 5 3.0 + 2 is: (i) 0 (ii) 0.0 (iii) 4 (iv) 4.0 G. The value of 5 * 2 + 1 is: (i) 2 (ii) 5 (iii) 6 (iv) none Exercise 3 What are the values of x, y, z and w after running all statements? int x=5, y, z=3, w; y = x z; z = 2 * y + 3; w = x 2 * y + z; z = w x; w++; Exercise 4 Evaluate the following expressions: A. 3 + 4 * 6 / 8 2 B. 3 + 5 * 6 / 4 3 C. 3 + 5.0 * 6 / 4 3 D. int x=2, y=4; double z=.0, w=12.0; a. x * w + z y; b. x > w c. (x>y) (z<w) d.!(x>w) && (z==w) 1

Exercise 5 Write a program that changes a given amount of money into smaller monetary units. The program lets the user enter an amount as a double value representing a total in Riyals and Halals, and outputs a report listing the monetary equivalent in the maximum number of riyals, halfs (SR 0.5), quarters (SR 0.25), qirsh (SR 0.5), and halalah (SR 0.01), in this order, to result in the minimum number of coins. Here is a sampler un: Enter an amount, for example, 11.88: 11.88 Your amount 11.56 consists of 11 riyals 1 halfs 1 quarters 2 qirshs 3 halalahs 2

Tutorial 03 Solutions Exercise 1: A. 1.8 2.10 3.4 4.5 B. 1. 25 / 4 is 6 2. 25 / 4.0 is 6.25 3. 3 + 2 is 32 4. 3 + 2 is 5 5. 3 * 2 / 4 is 1 6. 3.0 * 2 / 4 is 1.5 C. m * Math(r, 2) D. E. F. 1. 4 / (3 * (r + 34)) 9 *(a+b*c)+(3+d*(2+a))/(a+b*d) 2. 5.5 * Math.pow(r + 2.5, 2.5 + t) 14.0 3.0 6 G. No. double and float in java use rounding in expressing floating point numbers. Exercise 2: Exercise 3: x = 5 y = 2 z = 3 w = 9 Exercise 4: A. (i) B. (i) C. (ii) D. (iii) E. (ii) F. (iv) G. (i) A. (i) B. (i) C. (ii) D a. 2.5 D b. false D c. true D d. false 3

Exercise 5: Here are the steps in developing the program: A. Prompt the user to enter the amount as a decimal number, such as 11.88 B. Convert the amount (e.g., 11.88) into halalas (1188). C. Divide the halalas by 100 to find the number of riyals. Obtain the remaining halalas using the halalas remainder 100. D. Divide the remaining halalas by 50 to find the number of halfs. E. Divide the remaining halalas by 25 to find the number of quarters. F. Divide the remaining halalas by 5 to find the number of qirshs. G. The remaining halalas are the halalas (can not be divided). H. Display the result. import java.util.scanner; public class ComputeChange { public static void main(string[] args) { // Create a Scanner Scanner input = new Scanner(System.in); // Receive the amount System.out.print("Enter an amount in double, for example 11.88: "); double amount = input.nextdouble(); int remainingamount = (int) (amount * 100); // Find the number of one riyals int numberofoneriyals = remainingamount / 100; remainingamount = remainingamount % 100; // Find the number of halfs in the remaining amount int numberofhalfs = remainingamount / 50; remainingamount = remainingamount % 50; // Find the number of quarters in the remaining amount int numberofquarters = remainingamount / 25; remainingamount = remainingamount % 25; // Find the number of qirshs in the remaining amount int numberofqirshs = remainingamount / 5; remainingamount = remainingamount % 5; // Find the number of halalas in the remaining amount int numberofhalalas = remainingamount; // Display results System.out.println("Your amount " + amount + " consists of"); System.out.println(" " + numberofoneriyals + " riyals"); System.out.println(" " + numberofhalfs + " halfs "); System.out.println(" " + numberofquarters + " quarters"); System.out.println(" " + numberofqirshs + " qirshs"); System.out.println(" " + numberofhalalas + " halalas"); } } 4