Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers

Similar documents
VARIABLES & ASSIGNMENTS

Chapter 2 Input, Processing and Output. Hong Sun COSC 1436 Spring 2017 Jan 30, 2017

CHAPTER 3 BASIC INSTRUCTION OF C++

// Display the prediction. System.out.println("Ampara district sales prediction: $" + eastcoastsales);

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Chapter 2: Introduction to C++

CSc Introduction to Computing

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

The C++ Language. Arizona State University 1

Lecture 3 Tao Wang 1

Chapter 3 Structure of a C Program

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

3 The L oop Control Structure

Reviewing all Topics this term

AP Computer Science Unit 1. Programs

Computational Expression

Analyzing invoice data

Programming with Java

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

Applications. 72 Variables and Patterns

Multiply and Divide Integers

Section 2: Introduction to Java. Historical note

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

UNIT- 3 Introduction to C++

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

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

ECE 122 Engineering Problem Solving with Java

Objectives/Outcomes. Introduction: If we have a set "collection" of fruits : Banana, Apple and Grapes.

1 Short Answer (5 Points Each)

Introduction to Java Unit 1. Using BlueJ to Write Programs

Control Statements: Part 1

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

Chapter 2. Lexical Elements & Operators

Full file at

Algorithms. Abdelghani Bellaachia, CSCI 1121 Page: 1

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

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

Chapter 3 Selection Statements

Slide 1 CS 170 Java Programming 1 The Switch Duration: 00:00:46 Advance mode: Auto

BRANCHING if-else statements

Programming Languages & Translators. XML Document Manipulation Language (XDML) Language Reference Manual

Full file at

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

GENERAL MATH FOR PASSING

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

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

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Chapter 2, Part III Arithmetic Operators and Decision Making

YOLOP Language Reference Manual

Chapter 4 - Notes Control Structures I (Selection)

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Objectives. In this chapter, you will:

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

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

Objectives. Data Types (continued) Data Types 4. การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต น Fundamentals of Computer Science

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

4. If the following Java statements are executed, what will be displayed?

Introduction to C Programming

Introduction to Java Applications

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Exam 1. CSC 121 Spring Lecturer: Howard Rosenthal. March 1, 2017

CS110: PROGRAMMING LANGUAGE I

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

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

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

Gaddis: Starting Out with Java: From Control Structures through Objects, 6/e

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

C Programming

Final Exam Review (Revised 3/16) Math MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Fractions and decimals have opposites, just as integers do. For example, 5 8 and 2 5

Operators. Java operators are classified into three categories:

Full file at C How to Program, 6/e Multiple Choice Test Bank

The Warhol Language Reference Manual

COMP 202 Java in one week

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

7 th Pre-AP REVIEW for TEST1 1 st Six Weeks

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

Section 3.1 Variables, Expressions and Order of Operations

Basics of Java Programming

Logic & program control part 2: Simple selection structures

COMP 202. Java in one week

Chapter 2: Basic Elements of Java

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Introduction to Object-Oriented Programming

Console Input and Output

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Part V. Arrays and Pointers

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Operators in java Operator operands.

2.4 Solving Linear Equations

All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

1. Expand and simplify: 9. Determine the area of the triangle. a. 5 b. 2. Simplify: a. ( 3) c. ( 2) d. 4 ( 2) 3. e ( 3) ( 5)

Conditional Programming

Conditionals and Loops

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Language Reference Manual

การเขยนโปรแกรมพนฐาน ภาคการศ กษาท 1 ป การศ กษา การเข ยนโปรแกรมพ นฐาน ว ทยาการคอมพ วเตอร เบ องต วทยาการคอมพวเตอรเบองตน น

Transcription:

Assignment 1 Expressions Data Types Formatted Printing Variables Scanning CSC 123 Fall 2018 Answer Sheet Short Answers 1. Every complete statement ends with a c. a. period b. parenthesis c. semicolon d. ending brace 2. The following data 72 'A' "Hello World" 2.8712 are all examples of b. a. variables b. constants (also called literals) c. strings d. none of these 3. A group of statements, such as the contents of a class or a method, are enclosed in a. a. braces {} b. parentheses () c. brackets [] d. any of these will do 4. Which of the following are not valid assignment statements? (Indicate all that apply. (Assume that variables have been declared) b, c a. total = 9; b. 72 = amount; c. profit = 129 d. letter = 'W'; 5. The negation operator is a. a. unary b. binary c. ternary d. none of these 1

6. This key word is used to declare a named constant. c a. constant b. namedconstant c. final d. concrete 7. Which Scanner class method would you use to read a string as input? b a. nextstring b. nextline c. readstring d. getline 8. Which Scanner class method would you use to read a double as input? a a. nextdouble b. getdouble c. readdouble d. None of these; you cannot read a double with the Scanner class 9. True or False: A left brace in a Java program is always followed by a right brace later in the program. 10. True or False: A variable must be declared before it can be used. 11. True or False: Variable names may begin with a number. 12. True or False: You cannot change the value of a variable whose declaration uses the final key word. 13. True or False: If one of an operator s operands is a double, and the other operand is an int, Java will automatically convert the value of the double to an int. 14. What s the Output? Determine the output of the following code segments or point out the error. a. int num = 5; num = num++; System.out.println(num); //Ans.: 5 b. int num = 5; num = ++num; System.out.println(num); //Ans.: 6 c. int num = 5; num = num++ = ++num; System.out.println(num); //Ans.: ill-formed (can t assign to an expression 2

d. int num = 5; num /= 3; System.out.println(num); //Ans.: 1 e. int num = 5; System.out.println(num += 5); //Ans.: 10 f. int num = 5; System.out.println(++num + num++); //Ans.: 12 Programs. Use printf for all output. 1. ASCII Write a program that prints the letters of your name followed by the ASCII value of each letter. For example: K 75 m 109 e 101 This is with my name, you should use your own d 100 Another way d 100 With printf 3

d 100 Another way d 100 2. SalesTax Write a program that will ask the user to enter the amount of a purchase. The program should then compute the state and county sales tax. Assume the state sales tax is 4 percent and the county sales tax is 2 percent. The program should display the amount of the purchase, the state sales tax, the county sales tax, the total sales tax, and the total of the sale (which is the sum of the amount of purchase plus the total sales tax). Hint: Use the value 0.02 to represent 2 percent, and 0.04 to represent 4 percent. Please enter the Purchase Price: 42599.71 Purchase Price: 42599.71 State Sales Tax: 1703.99 County Sales Tax: 851.99 Total Sales Tax: 2555.98 Total Cost: 45155.69 Please enter the Purchase Price: 1.99 Purchase Price: 1.99 State Sales Tax: 0.08 County Sales Tax: 0.04 Total Sales Tax: 0.12 Total Cost: 2.11 3. RestaurantBill Write a program that computes the tax and tip on a restaurant bill. The program should ask the user to enter the charge for the meal. The tax should be 6.75 percent of the meal charge. The tip should be 20 percent of the total after adding the tax. Display the meal charge, tax amount, tip amount, and total bill on the screen. Please enter the cost of the meal: 75.00 Meal Cost: 75.00 Tax Amount: 5.06 Tip Amount: 16.01 Total Bill: 96.08 4

Please enter the cost of the meal: 193.57 Meal Cost: 193.57 Tax Amount: 13.07 Tip Amount: 41.33 Total Bill: 247.96 4. StockTransaction Last month Joe purchased some stock in Acme Software, Inc. Here are the details of the purchase: The number of shares that Joe purchased was 1,000. When Joe purchased the stock, he paid $32.87 per share. Joe paid his stockbroker a commission that amounted to 2% of the amount he paid for the stock. Two weeks later Joe sold the stock. Here are the details of the sale: The number of shares that Joe sold was 1,000. He sold the stock for $33.92 per share. He paid his stockbroker another commission that amounted to 2% of the amount he received for the stock. Write a program that displays the following information: The amount of money Joe paid for the stock. The amount of commission Joe paid his broker when he bought the stock. The amount that Joe sold the stock for. The amount of commission Joe paid his broker when he sold the stock. Display the amount of profit that Joe made after selling his stock and paying the two commissions to his broker. (If the amount of profit that your program displays is a negative number, then Joe lost money on the transaction.) Purchase Price: 32870.00 Commission When Bought: 657.40 Sale Price: 33920.00 Commission When Sold: 678.40 Joe's Profit: -285.80 5. Extract Digits Write a program that requests a 5-digit integer and displays the digits one at a time. For example, given 38145, you program should print: First digit: 3. Second digit: 8. Third digit: 1. Fourth digit: 4. Fifth digit: 5. Hint: Use the % and / operations to extract the digits. Enter a five digit integer number 53210 First digit: 5 Second digit: 3 5

Third digit: 2 Fourth digit: 1 Fifth digit: 0 Enter a five digit integer number 10802 First digit: 1 Second digit: 0 Third digit: 8 Fourth digit: 0 Fifth digit: 2 Enter a five digit integer number 321 (even works with less than 5 digits) First digit: 0 Second digit: 0 Third digit: 3 Fourth digit: 2 Fifth digit: 1 6. ExtractLength Write a program that reads in a length in inches. Then extract the number of yards, feet and remaining inches from the input. Print out the result. For instance, if the input is 50 then the output would look like this; 50 inches converts to 1 yard, 1 feet and 2 inches. Remember 36 in a yard, 12 in a foot. Please enter the number of inches: 36 36 inches converts to 1 yards, 0 feet and 0 inches Please enter the number of inches: 57 57 inches converts to 1 yards, 1 feet and 9 inches Please enter the number of inches: 21 21 inches converts to 0 yards, 1 feet and 9 inches 6