Chapter 2. Elementary Programming. Program Listings

Similar documents
Chapter 2 Elementary Programming

Chapter 2 Elementary Programming

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

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

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

Chapter 2 ELEMENTARY PROGRAMMING

CEN 414 Java Programming

Chapter 2. Elementary Programming

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

JAVA Programming Concepts

Chapter 2 Primitive Data Types and Operations. Objectives

Tutorial 03. Exercise 1: CSC111 Computer Programming I

Copyright Y. Daniel Liang 1. Here are the hints for selected Java REVEL Programming Projects.

Chapter 2 Elementary Programming

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

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

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

Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word. Chapter 1 Introduction to Computers, Programs, and Java

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

Elementary Programming

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

(b) This is a valid identifier in Java: _T_R-U_E_. (c) This is a valid identifier in Java: _F_A_L_$_E_

Welcome to the Primitives and Expressions Lab!

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

Selections. CSE 114, Computer Science 1 Stony Brook University

Basics of Java Programming variables, assignment, and input

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

Values in 2 s Complement

Elementary Programming

LAB 2.1 INTRODUCTION TO C PROGRAMMING

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

Chapter 10. Object-Oriented Thinking

1. Look carefully at the program shown below and answer the questions that follow.

Chapter 3. Selections. Program Listings

Fundamentals of Programming Data Types & Methods

Full file at

Full file at

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

Datatypes, Variables, and Operations

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

Primitive Data, Variables, and Expressions; Simple Conditional Execution

SELECTION IDIOMS. Here is a summary of appropriate selection idioms: Selection Idioms. Action Condition Construct to Use. Sequential if statements

CS110 Programming Language I. Lab 3: Java basics II (model answer) Dr. Hadil Shaiba

Chapter 7 Arithmetic

Console Input and Output

Big Java. Fifth Edition. Chapter 3 Fundamental Data Types. Cay Horstmann

Over and Over Again GEEN163

int: integers, no fractional part double: floating-point numbers (double precision) 1, -4, 0 0.5, , 4.3E24, 1E-14

Chapter 2: Review Exercise Solutions R2.1

AP Computer Science Unit 1. Programs

METHODS EXERCISES GuessNumber and Sample run SumAll Sample Run

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

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

Full download all chapters instantly please go to Solutions Manual, Test Bank site: testbanklive.com

IST 297D Introduction to Application Programming Chapter 4 Problem Set. Name:


Activity 1: Introduction

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

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

Topics. The Development Process

CS 2316 Individual Homework 1 Python Practice Due: Wednesday, August 28th, before 11:55 PM Out of 100 points

LAB 12: ARRAYS (ONE DIMINSION)

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

Example: Monte Carlo Simulation 1

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

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

Introduction to Computer Science, Winter Term Lab Exercise 6 Discussion:

Chapter 3 Selections. 3.1 Introduction. 3.2 boolean Data Type

Introduction to Programming Using Python Lecture 4. Dr. Zhang COSC 1437 Fall, 2018 October 11, 2018

Lecture 2: Operations and Data Types

Date: Dr. Essam Halim

Marking rubric for Assignment #2

Imperative and Object Oriented Programming. Tutorial 1. Charlie Abela Department of Artificial Intelligence

2/9/2012. Chapter Four: Fundamental Data Types. Chapter Goals

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

COMP 202 Java in one week

Project 1: How to Make One Dollar

CONDITIONAL EXECUTION

Exercise (Revisited)

Lab 6B Coin Collection

Chapter 3 Problem Solving and the Computer

Chapter 9 Objects and Classes. Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

COMP 202. Java in one week

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

Programming with Java

pset1: C Tommy MacWilliam Grading Getting Started pset1: C Style Pennies Greedy Chart September 11, 2011

Unit 2: Linear Functions

Sample input: Resultant output: Group: Sum is 129

Chapter 4 Fundamental Data Types. Big Java by Cay Horstmann Copyright 2009 by John Wiley & Sons. All rights reserved.

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

Ninth Annual University of Oregon Programming Competition

Chapter 8 Multidimensional Arrays

Control Statements: Part 1

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

Section we will not cover section 2.11 feel free to read it on your own

Grade 1 ISTEP+ T1 #1-4 ISTEP+ T1 #5

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Testing and Debugging

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

Transcription:

Chapter 2 Elementary Programming Program Listings

Contents Listing 2.1 Compute Area... 3 Listing 2.2 Compute Area With Console Input... 4 Listing 2.3 Compute Average... 5 Listing 2.4 Compute Area With Constant... 6 Listing 2.5 Display Time... 7 Listing 2.6 Fahrenheit To Celcius... 8 Listing 2.7 Show Current Time... 9 Listing 2.8 Sales Tax... 10 Listing 2.9 Compute Loan... 11 Listing 2.10 Compute Change... 12

Listing 2.1 Compute Area public class ComputeArea // Variable declarations double radius; double area; radius = 20.0; area = radius * radius * 3.14159; System.out.println("The area for the circle of radius " + radius + " is " + area);

Listing 2.2 Compute Area With Console Input public class ComputeAreaWithConsoleInput // Prompt the user to enter the radius System.out.println("Enter the radius: "); double radius = input.nextdouble(); // Calculate the area double area = radius * radius * 3.14159; // Display results System.out.println("The area for the cirlce of radius " + radius + " is " + area);

Listing 2.3 Compute Average public class ComputeAverage // Prompt the user to enter three numbers System.out.println("Enter three numbers: "); double number1 = input.nextdouble(); double number2 = input.nextdouble(); double number3 = input.nextdouble(); // Compute average double average = (number1 + number2 + number3) / 3.0; // Display results System.out.println("The average of " + number1 + " " + number2 + " " + number3 + " is " + average);

Listing 2.4 Compute Area With Constant public class ComputeAreaWithConstant final double PI = 3.14159; // Prompt the user to enter the radius System.out.println("Enter the radius: "); double radius = input.nextdouble(); // Calculate the area double area = radius * radius * PI; // Display results System.out.println("The area for the cirlce of radius " + radius + " is " + area);

Listing 2.5 Display Time public class DisplayTime // Prompt the user for input System.out.println("Enter an integer for seconds: "); int seconds = input.nextint(); int minutes = seconds / 60; int remainingseconds = seconds % 60; // Display results System.out.println(seconds + " seconds is " + minutes + " minutes and " + remainingseconds + " seconds");

Listing 2.6 Fahrenheit To Celcius public class FahrenheitToCelsius // Prompt the user for input System.out.println("Enter a degree in Fahrenheit: "); double fahrenheit = input.nextdouble(); double celsius = (5.0 / 9.0) * (fahrenheit - 32.0); // Display results System.out.println("Fahrenheit " + fahrenheit + " is " + celsius + " in Celsius");

Listing 2.7 Show Current Time public class ShowCurrentTime // Obtain time in milliseconds since Jam1, 1970 long totalmilliseconds = System.currentTimeMillis(); // Convert the total milliseconds into seconds long totalseconds = totalmilliseconds / 1000; // Obtain Seconds long currentsecond = totalseconds % 60; // Obtain Minutes long totalminutes = totalseconds / 60; long currentminute = totalminutes % 60; // Obtain Hours long totalhours = totalminutes / 60; long currenthour = totalhours % 24; // Display time System.out.println("Current time is " + currenthour + ":" + currentminute + ":" + currentsecond + " GMT");

Listing 2.8 Sales Tax public class SalesTax // Prompt the user for input System.out.println("Enter purchase amount: "); double purchaseamount = input.nextdouble(); double tax = purchaseamount * 0.06; // Display results System.out.println("Sales tax is $ " + (int) (tax * 100) / 100.0);

Listing 2.9 Compute Loan public class ComputeLoan // Prompt user for annual interest rate System.out.println("Enter annual interest rate, e.g., 7.25 %: "); double annualinterestrate = input.nextdouble(); double monthlyinterestrate = annualinterestrate / 1200; // Prompt user for length of the loan System.out.println("Enter number of years as an integer, e.g., 5: "); int numberofyears = input.nextint(); // Prompt user for the amount System.out.println("Enter loan amount, e.g., 120000.95: "); double loanamount = input.nextdouble(); // Calculate payment double monthlypayment = loanamount * monthlyinterestrate / (1-1 / Math.pow(1 + monthlyinterestrate, numberofyears * 12)); double totalpayment = monthlypayment * numberofyears * 12; //Display results System.out.println("The monthly payment is $ " + (int) (monthlypayment * 100) / 100.0); System.out.println("The total payment is $ " + (int) (totalpayment * 100) / 100.0);

Listing 2.10 Compute Change public class ComputeChange // Prompt user for the amount System.out.println("Enter an amount in double, e.g., 11.56: "); double amount = input.nextdouble(); int remainingamount = (int) (amount * 100); // Calculate dollars int numberofonedollars = remainingamount / 100; remainingamount %= 100; // Calculate Quarters int numberofquarters = remainingamount / 25; remainingamount %= 25; // Calculate Dimes int numberofdimes = remainingamount / 10; remainingamount %= 10; // Calculate Nickels int numberofnickels = remainingamount / 5; remainingamount %= 5; // Calculate Pennies int numberofpennies = remainingamount; // Display Results System.out.println("Your amount " + amount + " consists of "); System.out.println(" " + numberofonedollars + " dollars."); System.out.println(" " + numberofquarters + " quarters."); System.out.println(" " + numberofdimes + " dimes.");

System.out.println(" " + numberofnickels + " nickels."); System.out.println(" " + numberofpennies + " pennies.");