Program Logic to Java GEEN163



Similar documents
Sources: On the Web: Slides will be available on:

System.out.println("\nEnter Product Number 1-5 (0 to stop and view summary) :

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

6.1. Example: A Tip Calculator 6-1

J a v a Quiz (Unit 3, Test 0 Practice)

Arrays in Java. Working with Arrays

Conditionals (with solutions)

In this Chapter you ll learn:

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter 2: Elements of Java

Comp 248 Introduction to Programming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Introduction to Java

Programming Languages CIS 443

3/13/2012. Writing Simple C Programs. ESc101: Decision making using if-else and switch statements. Writing Simple C Programs

Lecture 1 Introduction to Java

Building Java Programs

Java Basics: Data Types, Variables, and Loops

Sample CSE8A midterm Multiple Choice (circle one)

Informatica e Sistemi in Tempo Reale

Conditional Statements Summer 2010 Margaret Reid-Miller

Topic 11 Scanner object, conditional execution

AP Computer Science Java Mr. Clausen Program 9A, 9B

AP Computer Science Java Subset

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java

Some Scanner Class Methods

Digital System Design Prof. D Roychoudhry Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Chulalongkorn University International School of Engineering Department of Computer Engineering Computer Programming Lab.

The programming language C. sws1 1

Management Information Systems 260 Web Programming Fall 2006 (CRN: 42459)

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Building Java Programs

Scanner. It takes input and splits it into a sequence of tokens. A token is a group of characters which form some unit.

First Bytes Programming Lab 2

While and Do-While Loops Summer 2010 Margaret Reid-Miller

Binary Number System. 16. Binary Numbers. Base 10 digits: Base 2 digits: 0 1

CS170 Lab 11 Abstract Data Types & Objects

Chapter 3. Input and output. 3.1 The System class

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10)

Goals. Unary Numbers. Decimal Numbers. 3,148 is s 100 s 10 s 1 s. Number Bases 1/12/2009. COMP370 Intro to Computer Architecture 1

LabVIEW Day 6: Saving Files and Making Sub vis

Digital Design. Assoc. Prof. Dr. Berna Örs Yalçın

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

Building Java Programs

Lecture 2 Notes: Flow of Control

5.2 Q2 The control variable of a counter-controlled loop should be declared as: a.int. b.float. c.double. d.any of the above. ANS: a. int.

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Lecture 2. Binary and Hexadecimal Numbers

Introduction to Java Applets (Deitel chapter 3)

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Chapter 8. Living with Java. 8.1 Standard Output

Reading Input From A File

Visual Logic Instructions and Assignments

Teaching Pre-Algebra in PowerPoint

CS 106 Introduction to Computer Science I

Computer Science 217

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

1 Introduction. 2 Overview of the Tool. Program Visualization Tool for Educational Code Analysis

Computer Programming I

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals

Moving from CS 61A Scheme to CS 61B Java

ALGORITHMS AND FLOWCHARTS

Stacks. Linear data structures

2.3 WINDOW-TO-VIEWPORT COORDINATE TRANSFORMATION

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

The While Loop. Objectives. Textbook. WHILE Loops

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

Part I. Multiple Choice Questions (2 points each):

PA2: Word Cloud (100 Points)

Crash Dive into Python

Line-based file processing

Exercises for Design of Test Cases

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O

Basic Programming and PC Skills: Basic Programming and PC Skills:

Repetition Using the End of File Condition

FEEG Applied Programming 5 - Tutorial Session

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Pseudo code Tutorial and Exercises Teacher s Version

Number Representation

Using Impatica for Power Point

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Binary Adders: Half Adders and Full Adders

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

1.3 Conditionals and Loops

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Introduction to Java Programming ITP 109 (2 Units) Fall 2015

Java Crash Course Part I

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

PIC 10A. Lecture 7: Graphics II and intro to the if statement

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Answers to Review Questions Chapter 7

Transcription:

Program Logic to Java GEEN163

In theory, theory and practice are the same. In practice, they're not. Yogi Berra

Founders' Day The Founders' Day Convocation is Thursday, March 21 from 10:00 12:00 in the Harrison Auditorium Classes are suspended during that time The lab quiz will be next week instead of this week

Exam The second exam will be in lecture on Friday, March 22 The exam will cover everything since the first exam If statements Loops Files A sample exam is available on Blackboard under course materials

Thinking About Programs If you do not know how to do something, it is very difficult to explain to someone else how to do it If you do not know how to solve a problem, it is very difficult to write a program to do it It is very useful to think about what a program must do and in what order the steps must be taken

Logical Ordering Some things must happen before others If you are going to read a number and display it, the read must come before the display

Variables Needed When you write a program, consider the variables necessary You will probably need a variable to hold each input You will probably need a variable to hold the result of a calculation

Loops and Ifs If a program has to do something many times, it will need a loop The parts of the program that are not repeated will be outside the loop If a program does something different sometimes, the program will have an if statement

What variables are needed?

Solution The program uses two variables biggest number

Problem Description Read a positive number. If the number is not greater than zero, display an error message and ask the user again.

Problem Description Read a positive number. If the number is not greater than zero, display an error message and ask the user again. This program only needs one variable Since we have not been told it is a whole number, it should be defined as a double

double number; Java Implementation

Analyzing the Problem Read a positive number. If the number is not greater than zero, display an error message and ask the user again. The program requires a decision, so there will be an Java if statement The program will do something multiple times, so there will be a loop

Analyzing the Problem Read a positive number. If the number is not greater than zero, display an error message and ask the user again. The first step in the program is reading a number Reading the number will have to be inside a loop since we are doing it again

Java Implementation double number; { System.out.print("Enter a number >"); number = keyboard.nextdouble(); }

Analyzing the Problem Read a positive number. If the number is not greater than zero, display an error message and ask the user again. The java if statement will test if the number is less than or equal to zero When the Java if is true (that is number <= 0) the program will display an error message Display is done with System.out.println

Java Implementation double number; { System.out.print("Enter a number >"); number = keyboard.nextdouble(); if (number <= 0) { System.out.println("Be positive!"); } }

Analyzing the Problem Read a positive number. If the number is not greater than zero, display an error message and ask the user again. The loop condition is the same as the if condition, number <= 0 We always want to execute the loop at least once to read the number. Therefore a do while loop is appropriate

Java Implementation double number; do { System.out.print("Enter a number >"); number = keyboard.nextdouble(); if (number <= 0) { System.out.println("Be positive!"); } } while (number <= 0);

Problem Description For a 400 pixel wide by 600 pixel tall picture, set the Red intensity to zero using setred(int x, int y, int intensity)

Analyzing the Problem For a 400 pixel wide by 600 pixel tall picture, set the Red intensity to zero using setred(int x, int y, int intensity) There will have to be a loop for the 400 x pixels and a loop for the 600 y pixels

What Sets All Red Pixels to Zero? click on the next slide for( x = 0; x < 400; x++) { for( y = 0; y < 400; y++) { setred(x,y,0); } } A for( x = 0; x < 400; x++) { setred(x,y,0); } for( y = 0; y < 400; y++) { setred(x,y,0); } B for( x = 0; x < 400; x++) { setred(x,y,0); for( y = 0; y < 400; y++) { setred(x,y,0); } C for( x = 0; setred(x,y,0); x+400) { for( x = 0; setred(x,y,0); x+600){ } } D

What Sets All Red Pixels to Zero? A. A B. B C. C D. D 0% 0% 0% 0% A B C D

How many calls will be made to setred? A. 600 B. 1000 C. 240,000 D. cannot be determined 0% 0% 0% 0% 600 1000 240000 cannot be dete...

Problem Description Read the first ten integers from the file "numbers.txt" and display them on the screen

Analyzing the Problem Read the first ten integers from the file "numbers.txt" and display them on the screen The first steps are to create a Scanner object to read from the file The last step should be to close the file

Java Implementation java.io.file dog = new java.io.file("numbers.txt"); Scanner cat = new Scanner( dog ); cat.close();

Analyzing the Problem Read the first ten integers from the file "numbers.txt" and display them on the screen There must be a loop that repeats 10 times Since we know how many times to loop, a for loops would be appropriate

Java Implementation java.io.file dog = new java.io.file("numbers.txt"); Scanner cat = new Scanner( dog ); for (int counter = 0; counter < 10; counter++) { } cat.close();

Analyzing the Problem Read the first ten integers from the file "numbers.txt" and display them on the screen nextint() can be used to read a number System.out.println can be used to display the number The read must come before the display

Java Implementation java.io.file dog = new java.io.file("numbers.txt"); Scanner cat = new Scanner( dog ); int bull; for (int counter = 0; counter < 10; counter++) { bull = cat.nextint(); System.out.println(bull); } cat.close();

Modify the Program Modify the program so that it will work correctly if there are less than 10 numbers in the file Work with your team to define the answer

Possible solution java.io.file dog = new java.io.file("numbers.txt"); Scanner cat = new Scanner( dog ); int bull; for (int counter = 0; counter < 10 && cat.hasnext(); counter++) { bull = cat.nextint(); System.out.println(bull); } cat.close();

Another Possible solution java.io.file dog = new java.io.file("numbers.txt"); Scanner cat = new Scanner( dog ); int bull; loop: for (int counter = 0; counter < 10; counter++) { if (!cat.hasnext()) { break loop; } bull = cat.nextint(); System.out.println(bull); } cat.close();

Flowcharts Flowcharts are a way to visually describe the logic of a program It can be helpful to write a flowchart for a program and then convert the flowchart to Java

Flowchart to Java input:word converts to word = keyboard.next(); output: word converts to System.out.println( word ); converts to while ( infile.hasnext() )

Flowchart to Java converts to if (word.endswith("y")) { } else { }

Problem Description Read an English word from a file and display the plural of the word For most words, you just add an "s" on the end If the word ends in "y", you change the "y" to "i" and add "es" We will ignore the many other rules

Making a word plural

Complete the Program Working with your team, complete this program to display the plural of the words java.io.file dog = new java.io.file("data.txt"); Scanner cat = new Scanner( dog ); String word, yword;

Possible Solution while (cat.hasnext()) { word = cat.next(); if ( cat.endswith( "y" )) { yword = cat.substring( 0, cat.length()-1 ); word = yword + "ies"; } else { word = word + "s"; } System.out.println( word ); }

format Method The java.io.printwriter class has two identical methods, format and printf, to format output format(string format, var1, var2, ) Writes the variables to the output as specified by the format string Very similar to printf in the C programming language

Format Descriptors The format string may contain text with descriptors located in it. The descriptors start with a percent sign, % followed optionally by a length and then a format type character format data type result 'd' 'f' int or long The result is formatted as an integer double or float s String The string The result is formatted as a decimal number

Output length You can specify a number between the % and the descriptor character to indicate the minimum number of characters to print You can specify the maximum number of digits to the right of the decimal point %minlength.maxprecisionf

Format Examples double e = 2.718281828459045; System.out.format("answer is %5.3f", e); will display answer is 2.718 System.out.format("answer is %7.4f", e); will display answer is 2.7183

What is displayed? double x = 10.0; double y = x * 2.0 / 3.0; System.out.printf( y is %6.3f, y); 25% 25% 25% 25% A. y is 10.000 B. y is 6.666 C. y is 6.667 D. y is 6.66666666 A. B. C. D.

Write the printf statement double bill, tax; tax = cost * 0.07; bill = cost + tax + handling; // Display the bill with two decimal digits

Founders' Day The Founders' Day Convocation is Thursday, March 21 from 10:00 12:00 in the Harrison Auditorium Classes are suspended during that time The lab quiz will be next week instead of this week

Exam The second exam will be in lecture on Friday, March 22 The exam will cover everything since the first exam If statements Loops Files A sample exam is available on Blackboard under course materials