ACS-1903 Basic program structure Feb 16, 2017

Similar documents
1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Chapter 1 Lab Algorithms, Errors, and Testing

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

Java with BlueJ. Ron McFadyen

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

Chapter 2: Input, Processing, and Output

Evaluating the Style of your programs

Basic Computation. Chapter 2

Variables and numeric types

Full file at

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Hello World. n Variables store information. n You can think of them like boxes. n They hold values. n The value of a variable is its current contents

Object-Oriented Programming in Java

Logic & program control part 2: Simple selection structures

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

CEN 414 Java Programming

SNS COLLEGE OF ENGINEERING

AP Computer Science Unit 1. Writing Programs Using BlueJ

Basic Computation. Chapter 2

Chapter 1: Why Program? Main Hardware Component Categories 8/23/2014. Main Hardware Component Categories: Why Program?

The name of our class will be Yo. Type that in where it says Class Name. Don t hit the OK button yet.

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

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

Fundamentals of Programming Session 4

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

Introduction to Java Unit 1. Using BlueJ to Write Programs

First Visual Basic Lab Paycheck-V1.0

Introduction to Computer Programming

Lecture 6. Assignments. Java Scanner. User Input 1/29/18. Reading: 2.12, 2.13, 3.1, 3.2, 3.3, 3.4

Chapter 1: Introduction to Computers and Programming

Over and Over Again GEEN163

Computational Expression

Robots. Byron Weber Becker. chapter 6

COMP 202 Java in one week

Using Classes. GEEN163 Introduction to Computer Programming

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

Problem Solving With Loops

AP Computer Science Unit 1. Writing Programs Using BlueJ

Problem Solving. Chapter Problem Solving. Analysis (inquiry, examination, study) Goal

Oct Decision Structures cont d

Chapter 4 Defining Classes I

Constants. Why Use Constants? main Method Arguments. CS256 Computer Science I Kevin Sahr, PhD. Lecture 25: Miscellaneous

Primitive Data Types: Intro

Introduction to Java Applications; Input/Output and Operators

Chapter 1. Introduction to Programming and Visual Basic Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 3: SEP. 13TH INSTRUCTOR: JIAYIN WANG

Lab # 2. For today s lab:

CS 302: Introduction to Programming

St. Edmund Preparatory High School Brooklyn, NY

I. Variables and Data Type week 3

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

Mr. Monroe s Guide to Mastering Java Syntax

Chapter 2 Part 2 Edited by JJ Shepherd, James O Reilly

COMP 202. Java in one week

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

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

CIS 3260 Intro. to Programming with C#

12/22/11. Java How to Program, 9/e. public must be stored in a file that has the same name as the class and ends with the.java file-name extension.

Darrell Bethea May 25, 2011

Department of Computer Science Purdue University, West Lafayette

Methods CSC 121 Fall 2016 Howard Rosenthal

Introduction to Programming Using Java (98-388)

Project 1. Java Control Structures 1/17/2014. Project 1 and Java Intro. Project 1 (2) To familiarize with

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

A+ Computer Science -

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Section 2: Introduction to Java. Historical note

Chapter 2 ELEMENTARY PROGRAMMING

Program Fundamentals

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

A variable is a name for a location in memory A variable must be declared

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

ICOM 4015: Advanced Programming

Introduction To Java. Chapter 1. Origins of the Java Language. Origins of the Java Language. Objects and Methods. Origins of the Java Language

AP Computer Science A Summer Assignment

Text User Interfaces. Keyboard IO plus

Defining Classes and Methods

Flow of Control. Chapter 3 Part 3 The Switch Statement

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

Programming Exercise 7: Static Methods

Fundamentals of Programming. Lecture 3: Introduction to C Programming

2.5 Another Application: Adding Integers

AP Computer Science Unit 1. Programs

Console Input and Output

5/3/2006. Today! HelloWorld in BlueJ. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont. HelloWorld in BlueJ, Cont.

Course Outline. Introduction to java

Methods CSC 121 Spring 2017 Howard Rosenthal

Chapter 4: Conditionals and Recursion

Computer Hardware. Java Software Solutions Lewis & Loftus. Key Hardware Components 12/17/2013

IT 374 C# and Applications/ IT695 C# Data Structures

Simple Java Programs. OOC 4 th Sem, B Div Prof. Mouna M. Naravani

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

FRAC: Language Reference Manual

Lecture Set 2: Starting Java

Fundamentals of Programming Data Types & Methods

Math Modeling in Java: An S-I Compartment Model

Java+- Language Reference Manual

Chapter III. Java Simple Data Types. Chapter III Topics

Lecture Set 2: Starting Java

The C++ Language. Arizona State University 1

Transcription:

Problem: Suppose we need a program to calculate someone s net pay. For input the program requires a name, gross pay, deductions and a tax rate. The output comprises the name, gross pay, deductions, taxes paid, and net pay. The program determines the taxes to be paid and the net pay (i.e. the processing) as follows: taxes paid is (gross pay deductions) * tax rate net pay is gross pay deductions taxes paid Variable names in Java cannot have spaces in them and so the usual names given for the above concepts are: taxespaid, grosspay, deductions, taxrate and netpay. The naming convention for variables is to start with lower case and then capitalize the first letter of subsequent words. The above calculations are expressed in Java using assignment statements: taxespaid = (grosspay deductions) * taxrate netpay = grosspay deductions taxespaid Java considers = to be the assignment operator. Whatever the right-hand-side evaluates to is assigned to the variable on the left-hand-side. The = operator has the lowest priority and so the assignment is the last thing to be done when statements like the above execute. A recommended programming practice is to develop code in small steps. In the following we show how someone could develop the code iteratively: 1. Add a little bit of code 2. Compile 3. Debug 4. Repeat the above until all requirements are implemented.

Step 1: Code the application class with no code in the main method. Create a new class in a Java project, and edit your code to be: Make sure the code you enter compiles with no syntax errors. Java has keywords such as public, class, static, and void that must be entered in lowercase. Punctuation comprises { and, ( and ), and [ and ] ; these must all appear in pairs.

Step 2: Get the user s name and display the name. We will use standard input and output for this. That is, we will use a Scanner object and System.out. To get the user s name we first of all prompt the user and then get the value entered. The code below does this note new lines are highlighted in bold text: There are several points to note about the highlighted lines above: The first line is called an import statement. It is necessary to include as we must inform the compiler where to find a definition of the Scanner class. The lines that begin // are single-line comments. Their purpose is to help humans read code these lines are ignored by a compiler. Some programmers will use these to help others understand how the code has been organized. The line declares a variable named keyboard to be of type Scanner. On the right-hand-side of the = sign is how one instantiates a Scanner object that is standard input. The line causes output to be written to the standard output device (BlueJ s terminal window). Obviously this informs the user to use the keyboard to enter their name. The next line is an assignment statement: When this line executes the expression keyboard.next() must be evaluated. This expression has no value until the user types on the keyboard and presses the enter button. So the effect is that the program is paused until the moment the user completes his/her action. The line displays the value of the expression enclosed in parentheses on the standard output device. Note the argument being passed in to the println method is "Name: "+name In this situation the + operator is called string catenation (one string is appended to the other). The + operator works differently depending on the type involved (numbers will be added together, strings will be catenated).

Step 3: Get the gross pay from the user. To add this functionality to our program we need another prompt and another action to get the gross pay from the user. Suppose a user might enter gross pay with a decimal point (e.g. 1500.25); to allow this we will use the double datatype for gross pay, and the nextdouble method of the Scanner class. The code below does this note the new lines are highlighted in bold text: Something to note about the lines above: The line uses the nextdouble method of the Scanner class. If a user might supply a numeric value that has a decimal point we must use this method.

Step 4: Get the deductions from the user. The code to do this is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); System.out.println("Deductions: "+grosspay);

Step 5: Get the tax rate from the user. The code to do this is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); // get tax rate System.out.println("enter the tax rate:"); double taxrate = keyboard.nextdouble(); System.out.println("Deductions: "+deductions);

Step 6: Calculate and display the taxes to be paid. The code to do this is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); // get tax rate System.out.println("enter the tax rate:"); double taxrate = keyboard.nextdouble(); // calculate the taxes to be paid double taxespaid = (grosspay - deductions) * taxrate; System.out.println("Deductions: "+deductions); System.out.println("Taxes Paid: "+taxespaid); The statement (grosspay deductions) * taxrate; shows the use of the * operator for multiplication and the use of parentheses to create a subexpression. Sub-expressions are always evaluated first. Can you see why a sub-expression is necessary here?

Step 7: Calculate and display the net pay. The added code is highlighted below: // get deductions System.out.println("enter deductions:"); double deductions = keyboard.nextdouble(); // get tax rate System.out.println("enter the tax rate:"); double taxrate = keyboard.nextdouble(); // calculate the taxes to be paid double taxespaid = (grosspay - deductions) * taxrate; // calculate the net pay double netpay = grosspay - deductions - taxespaid; System.out.println("Deductions: "+deductions); System.out.println("Taxes Paid: "+taxespaid); System.out.println("Net pay: "+netpay);