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



Similar documents
Sample CSE8A midterm Multiple Choice (circle one)

AP Computer Science Static Methods, Strings, User Input

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

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

Introduction to Java

Chapter 2 Introduction to Java programming

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

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

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

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

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

Topic 11 Scanner object, conditional execution

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane.

Moving from CS 61A Scheme to CS 61B Java

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

AP Computer Science Java Subset

Informatica e Sistemi in Tempo Reale

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

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

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

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard

java.util.scanner Here are some of the many features of Scanner objects. Some Features of java.util.scanner

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

Chapter 3. Input and output. 3.1 The System class

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

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

CS 106 Introduction to Computer Science I

Building Java Programs

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

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

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc.

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

Chapter 2: Elements of Java

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

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

Using Files as Input/Output in Java 5.0 Applications

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

Java Basics: Data Types, Variables, and Loops

In this Chapter you ll learn:

Chapter 2 Elementary Programming

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

Building Java Programs

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand:

Programming in Java Course Technology, a part of Cengage Learning.

Homework/Program #5 Solutions

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

Programming and Data Structures with Java and JUnit. Rick Mercer

Java Crash Course Part I

Introduction to Programming

Object Oriented Software Design

Introduction to Java. CS 3: Computer Programming in Java

LOOPS CHAPTER CHAPTER GOALS

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

13 File Output and Input

CS 106 Introduction to Computer Science I

Building Java Programs

Introduction to Computer Programming, Spring Term 2014 Practice Assignment 3 Discussion

Programming Languages CIS 443

Computer Programming I

Explain the relationship between a class and an object. Which is general and which is specific?

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

Basics of Java Programming Input and the Scanner class

Visual Logic Instructions and Assignments

Example of a Java program

Software development and programming. Software

Continuous Integration Part 2

Debugging. Common Semantic Errors ESE112. Java Library. It is highly unlikely that you will write code that will work on the first go

Lecture 1 Introduction to Java

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75

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

Chapter 6: Programming Languages

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

Lecture 2 Notes: Flow of Control

Programmierpraktikum

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

Install Java Development Kit (JDK) 1.8

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Welcome to Introduction to programming in Python

We will learn the Python programming language. Why? Because it is easy to learn and many people write programs in Python so we can share.

VHDL Test Bench Tutorial

1.00/ Session 2 Fall Basic Java Data Types, Control Structures. Java Data Types. 8 primitive or built-in data types

6.1. Example: A Tip Calculator 6-1

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

JAVA ARRAY EXAMPLE PDF

Statements and Control Flow

WESTMORELAND COUNTY PUBLIC SCHOOLS Integrated Instructional Pacing Guide and Checklist Computer Math

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.

Introduction to Python

Visual Basic Programming. An Introduction

Some Scanner Class Methods

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

Iteration CHAPTER 6. Topic Summary

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

Stacks. Linear data structures

Course Intro Instructor Intro Java Intro, Continued

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

Transcription:

Translation Translating to Java Introduction to Computer Programming The job of a programmer is to translate a problem description into a computer language. You need to be able to convert a problem description into an algorithm and the algorithm into Java. Many Level Translations Requirements Design Algorithm Java Machine Language Input Programs usually get some kind of input from the user. Input for our programs will usually be from the keyboard. More advanced programs can get their input from files, network or Graphical User Interface (GUI). Problem descriptions usually use these words for input: read, get, input, ask, request

Java Keyboard Input The Scanner class is used for input java.util.scanner in = new java.util.scanner(system.in); anint = in.nextinteger(); The program will wait for the user to enter data. Java Dialog Box Input The JOptionPane class can be used for input with a pop up dialog box. reply = javax.swing.joptionpane.showinputdialog( Enter something ); A box will pop up for every input. The input is always a String which may need to be converted to a double or int. Output Programs generally display some kind of output or you will not know what it did. In our class the output has been to the console or pop up boxes. More advanced programs output to files, network or GUIs. Problem descriptions usually use these words for output: display, write, show, print Java Console Output The System.out.println method can be used for output to the console. System.out.println( message ); System.out.print( enter stuff> ); The output isn t pretty, but it is easy to do.

Java Dialog Box Output The JOptionPane class can be used for output with a pop up dialog box. javax.swing.joptionpane. showmessagedialog(null, Something ) Outputting Text and Variables Frequently you will want to output the value of a variable in the middle of an English sentence. System.out.println( x is +avg); This is useful for outputting a single value. Text is surrounded by quotes. Variables are not. Decisions Most programs will need to make decisions based on a comparison of variables. Problem descriptions usually use these words for decisions: when, if, unless Java IF statement The if statement in Java allows the programmer to choose different paths of execution. Based on a comparison of variable values, the program can do different things. An if statement changes the flow of control

If Syntax if ( Logical Expression ) Statement NOTE: Statement can be a single statement or a block. If-Then statement is a selection of whether or not to execute a statement (which can be a single statement or an entire block) TRUE statement expression FALSE If Examples double mph; mph = in.nextdouble(); if (mph > 55.0){ System.out.println( tickets ); if (mph < 45.0) { System.out.println( snail ); If-Then-Else Syntax if ( Logical Expression ) else StatementA StatementB NOTE: StatementA and StatementB each can be a single statement, or a block.

if... else provides two-way selection between executing one of 2 clauses (the if clause or the else clause) TRUE if clause expression FALSE else clause Use of Brackets Recommended if ( Logical Expression ) { statements; else { other statements; when true when false Comparison Operators Boolean Operators Operator Name Operator Name < less than <= less than or equal to > greater than >= greater than or equal to == equal to! not && and or ^ exclusive or!= not equal to

Convert these to Java double quarters; String word; int width; double cx, cy; quarters is greater than zero width plus the length of the word is greater than 25 word does not contain DATA Possible Solutions quarters > 0 width + word.length() > 25 word.indexof( DATA ) == -1 cx 2 + cy 2 r Math.sqrt(cx*cx+cy*cy) <= r Repetition Many programs will repeat parts of the program in a loop. Problem descriptions usually use these words for loops: while, repeat, until Java While Statement while ( Expression ) {.. // loop body. NOTE: Loop body can be a single statement or a block.

When the expression is tested and found to be false, the loop is exited and control passes to the statement which follows the loop body. WHILE LOOP FALSE Expression TRUE body statement Program Logic while ( Expression ){ // the loop body must change the value // of a variable in the Expression After the loop, you know that the Expression is false. Variables and Calculations Most of our programs will calculate something. Many times a program will just move the value of one variable to another. startime = now; count = 1; Java Variable Declarations You have to tell Java what variables your program will be using. You have to define the type of the variable type var1, var2; where type is int, double, String, etc. int mythings; double here2there;

Java Assignment Statement An assignment statement gives a variable a value; Translating from Math to Java 2 diam = πr diam = 3.14159265 * r * r; var1 = 47; var2 = 23.25 + var3 / 17.0; x = a + b c* d x=(a+b)/(c*d); p = n* r * t v p = n * r * t / v; Integer Division Integers can only hold whole numbers If division results in a fractional part, the fraction is dropped and the result is just the whole number. 8/2 is 4 7 / 3 is 2 1 / 2 is 0 5 / 4 is 1 Operator Priority Precedence Operator Description Higher ( ) Parenthesis + Positive - Negative * Multiplication / Division % Modulus (remainder) + Addition - Subtraction Lower = Assignment

Put the Program in the Template public class Whatever { public static void main(string[] a) { // variable declarations here // The rest of the program here