Programming Fundamentals I CS 110, Central Washington University. November 2015

Size: px
Start display at page:

Download "Programming Fundamentals I CS 110, Central Washington University. November 2015"

Transcription

1 Programming Fundamentals I CS 110, Central Washington University November 2015

2 Next homework, #4, was due tonight! Lab 6 is due on the 4 th of November Final project description + pseudocode are due 4th of November, tomorrow

3 Chapter 5 : sections 5.3 and 5.4

4 Poll Question review Today we have two intro speed poll questions You will be given 20 seconds to see each question The main method will be highlighted Both questions are the same, What does this program do? You will then be presented with the answer choices, and you'll have 60 seconds to answer The first question get ready

5 Poll Question review

6 Poll Question review What does the program on the previous slide do? A. Determine how much money user has in bank, print a message, set loan percentage, receive money B. Get user's bank account balance, if balance is zero, do not offer loan C. Determine how much money user has in bank, set loan percentage, withdraw money from account D. Get user's bank account balance, calculate loan percentage, and inform user of Loan Amount E. Determine how much money user has in bank, if balance more than 1000, then sent welcome gift F. Get user's bank account balance from keyboard, calculate loan percentage, inform user of Loan amount G. Get user's bank account balance from mouse clicks, calculate loan, inform user of Loan amount

7 Poll Question review The second question

8 Poll Question review

9 Poll Question review What does the program on the previous slide do? A. Determine how much money user has in bank, print a message, set loan percentage, receive money B. Get user's bank account balance, if balance is zero, do not offer loan C. Determine how much money user has in bank, set loan percentage, withdraw money from account D. Get user's bank account balance, calculate loan percentage, and inform user of Loan Amount E. Determine how much money user has in bank, if balance more than 1000, then sent welcome gift F. Get user's bank account balance from keyboard, calculate loan percentage, inform user of Loan amount G. Get user's bank account balance from mouse clicks, calculate loan, inform user of Loan amount Lesson Learned: even if you weren't able to select the correct answer, the second program was simpler to read you didn't need to concern yourself with details

10 What was at first a complicated program (running a city), has become a much simpler endeavor. AdayInTheLifeOfACity{ Finance Controller maintains checkbooks Finance Controller prints payroll checks Finance Controller acquires loan from the feds Finance Controller increase pension dollars... Parks and Rec commissioner fixes roads Parks and Rec commissioner mows the park's lawn Parks and Rec commissioner plants new trees Parks and Rec commissioner cleans city swimming pool... Education Superintendent hires 12 new teachers Education Superintendent fires 13 teachers Education Superintendent hires 2 new teachers Education Superintendent approves school lunch menu Education Superintendent cancels after school progs. Education Superintendent has nervous breakdown... Head Doctor buys a new MRI machine Head Doctor goes golfing Head Doctor performs first ever brain transplant Head Doctor discovers cure to the common cold... Judge raises taxes on people younger than 2 years old Judge repeals pharmacy medicinal marijuana licenses Judge purchases a new fake wig with weird curls Judge orders adding an 8th day to the week AdayInTheLifeOfACity{ runfinancedepartment(){... maintainparks(){... Finance Controller maintains checkbooks Finance Controller prints payroll checks Finance Controller acquires loan from the feds Finance Controller increase pension dollars... Parks and Rec commi ssioner fixes roads Parks and Rec commissioner mows the park's lawn Parks and Rec commi ssioner plants new trees Parks and Rec commi ssioner cleans city swimming poo... Education Superintendent hires 12 new teachers Education Superintendent fires 13 teachers Education Superintendent hires 2 new teachers Education Superintendent approves school lunch menu Education Superintendent cancels after school progs. Education Superintendent has nervous breakdown... Head Doctor buys a new MRI machine Head Doctor goe s golfing Head Doctor perform s first ever brain transplant Head Doctor discovers cure to the common cold... Judge raise s taxes on people younger than 2 years old Judge repeals pharmacy medicinal marijuana license s Judge purcha se s a new fake wig with weird curls Judge orders adding an 8th day to the week teachfeeddisiplinestudents(){... maintainthecityshealth(){... interpretandenforcethelaws(){...

11 You want to write a simple program that prompts the user for input (integer), and then squares the result // import statements import java.util.scanner; The Non-methods version... public class ProgramWithoutMethods { // the main routine public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output);

12 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); The method's header, which declares the method Goal: Simplify this program to use methods public static void main (String[] args) { displayinstructions(); Method modifiers Return type void is a java keyword: does not return Method Name Parentheses empty parentheses means does not take any arguments Body of the method Curly Brackets enclose...

13 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); Method modifiers The java keyword return indicates return THIS value Curly Brackets enclose... Return type Method Name int The item being returned must be of the appropriate type: Body of the method empty parentheses means does not take any arguments

14 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); These are? Method Modifiers This specifies? This is? The return type The Method Name Non-empty parentheses designate? the argument(s) that this method expects to receive Here a single argument is provided, input, of type int Values passed into a method are called arguments, and/or parameters

15 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); What does this method's body code block do? What is the purpose of this statement? It indicates that this method returns output, which is of type int

16 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); The variable output has been declared in the code block of the method squareavalue, so that variable is accessible only WHITHIN this code block. We say that the variable output has scope only within the code block of the method squareavalue The variable input is an argument to the method, so it is NOT declared in the body of the method, and we CANNOT declare a variable in the body of the method with the same name.

17 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); printintvalue(calculationresult);? Here, valtoprint is not declared inside of the code block because it is being passed TO the method as an argument

18 public static void main (String[] args) { System.out.println("Hello, this program performs " + "a simple calculation"); System.out.print("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); System.out.println("You've supplied " + userinput); System.out.println("The input to squareaval is " + userinput); int output = userinput * userinput; System.out.println("The square of userinput is: " + output); System.out.println("The final result is: " + output); public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); printintvalue(calculationresult); The version without methods What have we accomplished in rewriting the program? The version with methods The program with methods is easier to read The program with methods is modular, in that each method's name clearly designates what that part of the program is doing The dependency of the parts of the program (squareavalue is dependent on the user's input), is easy to see Okay fine but WHERE are the methods that's we've written?

19 // import statements import java.util.scanner; public class ProgramWithMethods { // a method that prints instructions public static void displayinstructions(){ System.out.println("Hello, this program performs a simple calculation"); // a method that queries a user for a number and returns value public static int getusersinput(){ System.out.println("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); return userinput; // a method that performs a calculation and returns the result public static int squareavalue(int input){ int output = input * input; return output; // a method that prints a value (the argument) public static void printintvalue(int valtoprint){ System.out.println("The argument passed to printintval is: " + valtoprint); // the main routine public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); printintvalue(calculationresult); Method that prints instructions Method that gets input from keyboard and returns it as an int Method that squares a value Method that prints a value main method

20 is where the program begins // the main routine public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); printintvalue(calculationresult); Method call (or invocation) that returns nothing and takes no arguments Method call (or invocation) that returns an integer and takes no arguments Method call (or invocation) that returns an integer and takes an argument Method call (or invocation) that returns nothing (void) and takes an argument

21 // import statements import java.util.scanner; public class ProgramWithMethods { // a method that prints instructions public static void displayinstructions(){ System.out.println("Hello, this program performs a simple calculation"); // a method that queries a user for a number and returns value public static int getusersinput(){ System.out.println("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); return userinput; // a method that performs a calculation and returns the result public static int squareavalue(int input){ int output = input * input; return output; // a method that prints a value (the argument) public static void printintvalue(int valtoprint){ System.out.println("The argument passed to printintval is: " + valtoprint); // the main routine public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); printintvalue(calculationresult); When the program starts, it begins in the main method The first statement is a call to a method So the program proceeds to execute that method When that method completes... The program resumes executing that part of the main method where it was last Which is a declaration and assignment, that calls another method So the program executes that method... And a scanner is created... A new variable is declared and assigned a value taken from the keyboard And the return keyword indicates, return the value in the variable userinput Later we will learn how methods can return things other than just integers or values of variables As soon as this method completes, then the variable userinput is REMOVED from the computer's memory The variable userinput is a local variable, that is accessible ONLY to the code block where it was declared

22 // import statements import java.util.scanner; public class ProgramWithMethods { // a method that prints instructions public static void displayinstructions(){ System.out.println("Hello, this program performs a simple calculation"); // a method that queries a user for a number and returns value public static int getusersinput(){ System.out.println("Please input an integer: "); Scanner keyboard = new Scanner(System.in); int userinput = keyboard.nextint(); return userinput; // a method that performs a calculation and returns the result public static int squareavalue(int input){ int output = input * input; return output; // a method that prints a value (the argument) public static void printintvalue(int valtoprint){ System.out.println("The argument passed to printintval is: " + valtoprint); // the main routine public static void main (String[] args) { displayinstructions(); int userinput = getusersinput(); int calculationresult = squareavalue(userinput); printintvalue(calculationresult); When the program starts, it begins in the main method And the program resumes executing the main method where it left off last Which is a declaration of a variable, and an assignment, that is the output of a method that takes a single argument That method is executed, variables that have scope ONLY in that method are created... And this method returns the value stored in the variable output and the main method resumes were the program last left off which involves another method. Which includes an argument, and a single System.out.println statement... Program execution returns to the point in the main method where it was last Program terminates

23 Q: Is that s it? Are methods good only for making the code easier to read??

24 Assume you want a program that will ask a user to provide the amount that he/she has in a bank account, and then performs an interest calculations once, twice, or three times. The program without methods might look like the following: Receive input from the user Ask user if he/she wants to perform interest calculation Notice the redundant code This entire program can be rewritten using methods... import java.util.scanner; public class ANonMethodProgram { // main routine public static void main (String[] args){ // create scanner and get starting amount Scanner scan = new Scanner(System.in); double startingamount=0.0, updatedamount=0.0; System.out.print(" What is the starting bank account value? "); startingamount = scan.nextdouble(); scan.nextline(); // ask three times if user wants to compound amount System.out.println("Perform interest (3.4%) calculation? Enter y for yes, and n for no: "); if (scan.nextline().equals("y")){ updatedamount = startingamount * 1.034; System.out.println("The new accnount value is: " + updatedamount); System.out.println("Perform interest (3.4%) calculation? Enter y for yes, and n for no: "); if (scan.nextline().equals("y")){ updatedamount = updatedamount * 1.034; System.out.println("The new accnount value is: " + updatedamount); System.out.println("Perform interest (3.4%) calculation? Enter y for yes, and n for no: "); if (scan.nextline().equals("y")){ updatedamount = updatedamount * 1.034; System.out.println("The new accnount value is: " + updatedamount);

25 import java.util.scanner; public class MethodsAreGood { The method getusersinput is written once and if it is working, you can think of it as an autonomous little program that you don't have to worry about.. and maybe reuse elsewhere The method compoundinterest is written once and reused multiple times // get user's initial value of account public static double getusersinput(){ Scanner scan = new Scanner(System.in); System.out.print(" What is the starting bank account value? "); return scan.nextdouble(); // compound value public static double compoundinterest(double value){ Scanner scan = new Scanner(System.in); System.out.println("Perform interest (3.4%) calculation? Enter y for yes, and n for no: "); if (scan.nextline().equals("y")){ value = value * 1.034; System.out.println("The new account value is: " + value); return value; // main routine public static void main (String[] args){ // compound interest double accountvalue = getusersinput(); accountvalue = compoundinterest(accountvalue); accountvalue = compoundinterest(accountvalue); accountvalue = compoundinterest(accountvalue);

26

27 More Methods Chapter 5 Begin Chapter 6

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html

Install Java Development Kit (JDK) 1.8 http://www.oracle.com/technetwork/java/javase/downloads/index.html CS 259: Data Structures with Java Hello World with the IntelliJ IDE Instructor: Joel Castellanos e-mail: joel.unm.edu Web: http://cs.unm.edu/~joel/ Office: Farris Engineering Center 319 8/19/2015 Install

More information

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq Introduction to Programming using Java wertyuiopasdfghjklzxcvbnmqwertyui

More information

Basics of Java Programming Input and the Scanner class

Basics of Java Programming Input and the Scanner class Basics of Java Programming Input and the Scanner class CSC 1051 Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website: www.csc.villanova.edu/~map/1051/

More information

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

Scanner sc = new Scanner(System.in); // scanner for the keyboard. Scanner sc = new Scanner(System.in); // scanner for the keyboard INPUT & OUTPUT I/O Example Using keyboard input for characters import java.util.scanner; class Echo{ public static void main (String[] args) { Scanner sc = new Scanner(System.in); // scanner for the keyboard

More information

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

WA2099 Introduction to Java using RAD 8.0 EVALUATION ONLY. Student Labs. Web Age Solutions Inc. WA2099 Introduction to Java using RAD 8.0 Student Labs Web Age Solutions Inc. 1 Table of Contents Lab 1 - The HelloWorld Class...3 Lab 2 - Refining The HelloWorld Class...20 Lab 3 - The Arithmetic Class...25

More information

Introduction to Java. CS 3: Computer Programming in Java

Introduction to Java. CS 3: Computer Programming in Java Introduction to Java CS 3: Computer Programming in Java Objectives Begin with primitive data types Create a main class with helper methods Learn how to call built-in class methods and instance methods

More information

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

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Handout 1 CS603 Object-Oriented Programming Fall 15 Page 1 of 11 Handout 1 Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner. Java

More information

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

Chapter 2. println Versus print. Formatting Output withprintf. System.out.println for console output. console output. Console Input and Output Chapter 2 Console Input and Output System.out.println for console output System.out is an object that is part of the Java language println is a method invoked dby the System.out object that can be used

More information

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

Introduction to Java Applications. 2005 Pearson Education, Inc. All rights reserved. 1 2 Introduction to Java Applications 2.2 First Program in Java: Printing a Line of Text 2 Application Executes when you use the java command to launch the Java Virtual Machine (JVM) Sample program Displays

More information

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

1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius Programming Concepts Practice Test 1 1) Which of the following is a constant, according to Java naming conventions? a. PI b. Test c. x d. radius 2) Consider the following statement: System.out.println("1

More information

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

System.out.println(\nEnter Product Number 1-5 (0 to stop and view summary) : Benjamin Michael Java Homework 3 10/31/2012 1) Sales.java Code // Sales.java // Program calculates sales, based on an input of product // number and quantity sold import java.util.scanner; public class

More information

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

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account). IRA EXAMPLES This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account). Definite Counting Loop Example IRA After x Years This first example illustrates

More information

Introduction to Java

Introduction to Java Introduction to Java The HelloWorld program Primitive data types Assignment and arithmetic operations User input Conditional statements Looping Arrays CSA0011 Matthew Xuereb 2008 1 Java Overview A high

More information

Chapter 2: Elements of Java

Chapter 2: Elements of Java Chapter 2: Elements of Java Basic components of a Java program Primitive data types Arithmetic expressions Type casting. The String type (introduction) Basic I/O statements Importing packages. 1 Introduction

More information

Comp 248 Introduction to Programming

Comp 248 Introduction to Programming Comp 248 Introduction to Programming Chapter 2 - Console Input & Output Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal, Canada These slides has been

More information

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

Chulalongkorn University International School of Engineering Department of Computer Engineering 2140105 Computer Programming Lab. Chulalongkorn University Name International School of Engineering Student ID Department of Computer Engineering Station No. 2140105 Computer Programming Lab. Date Lab 2 Using Java API documents, command

More information

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

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. Exam Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question. 1) The JDK command to compile a class in the file Test.java is A) java Test.java B) java

More information

Introduction to Programming

Introduction to Programming Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems sjmaybank@dcs.bbk.ac.uk Spring 2015 Week 2b: Review of Week 1, Variables 16 January 2015 Birkbeck

More information

Using Files as Input/Output in Java 5.0 Applications

Using Files as Input/Output in Java 5.0 Applications Using Files as Input/Output in Java 5.0 Applications The goal of this module is to present enough information about files to allow you to write applications in Java that fetch their input from a file instead

More information

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

CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals CSE 1223: Introduction to Computer Programming in Java Chapter 2 Java Fundamentals 1 Recall From Last Time: Java Program import java.util.scanner; public class EggBasket { public static void main(string[]

More information

AP Computer Science Static Methods, Strings, User Input

AP Computer Science Static Methods, Strings, User Input AP Computer Science Static Methods, Strings, User Input Static Methods The Math class contains a special type of methods, called static methods. A static method DOES NOT operate on an object. This is because

More information

Chapter 3. Input and output. 3.1 The System class

Chapter 3. Input and output. 3.1 The System class Chapter 3 Input and output The programs we ve looked at so far just display messages, which doesn t involve a lot of real computation. This chapter will show you how to read input from the keyboard, use

More information

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

Part I. Multiple Choice Questions (2 points each): Part I. Multiple Choice Questions (2 points each): 1. Which of the following is NOT a key component of object oriented programming? (a) Inheritance (b) Encapsulation (c) Polymorphism (d) Parallelism ******

More information

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

AP Computer Science Java Mr. Clausen Program 9A, 9B AP Computer Science Java Mr. Clausen Program 9A, 9B PROGRAM 9A I m_sort_of_searching (20 points now, 60 points when all parts are finished) The purpose of this project is to set up a program that will

More information

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

File class in Java. Scanner reminder. Files 10/19/2012. File Input and Output (Savitch, Chapter 10) File class in Java File Input and Output (Savitch, Chapter 10) TOPICS File Input Exception Handling File Output Programmers refer to input/output as "I/O". The File class represents files as objects. The

More information

Some Scanner Class Methods

Some Scanner Class Methods Keyboard Input Scanner, Documentation, Style Java 5.0 has reasonable facilities for handling keyboard input. These facilities are provided by the Scanner class in the java.util package. A package is a

More information

Introduction to Object-Oriented Programming

Introduction to Object-Oriented Programming Introduction to Object-Oriented Programming Programs and Methods Christopher Simpkins chris.simpkins@gatech.edu CS 1331 (Georgia Tech) Programs and Methods 1 / 8 The Anatomy of a Java Program It is customary

More information

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following: In creating objects of the type, we have used statements similar to the following: f = new (); The parentheses in the expression () makes it look like a method, yet we never created such a method in our

More information

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

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language 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

More information

Sample CSE8A midterm Multiple Choice (circle one)

Sample CSE8A midterm Multiple Choice (circle one) Sample midterm Multiple Choice (circle one) (2 pts) Evaluate the following Boolean expressions and indicate whether short-circuiting happened during evaluation: Assume variables with the following names

More information

Object-Oriented Programming in Java

Object-Oriented Programming in Java CSCI/CMPE 3326 Object-Oriented Programming in Java Class, object, member field and method, final constant, format specifier, file I/O Dongchul Kim Department of Computer Science University of Texas Rio

More information

6.1. Example: A Tip Calculator 6-1

6.1. Example: A Tip Calculator 6-1 Chapter 6. Transition to Java Not all programming languages are created equal. Each is designed by its creator to achieve a particular purpose, which can range from highly focused languages designed for

More information

Lab 5: Bank Account. Defining objects & classes

Lab 5: Bank Account. Defining objects & classes Lab 5: Bank Account Defining objects & classes Review: Basic class structure public class ClassName Fields Constructors Methods Three major components of a class: Fields store data for the object to use

More information

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python

University of Hull Department of Computer Science. Wrestling with Python Week 01 Playing with Python Introduction Welcome to our Python sessions. University of Hull Department of Computer Science Wrestling with Python Week 01 Playing with Python Vsn. 1.0 Rob Miles 2013 Please follow the instructions carefully.

More information

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

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4 JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4 NOTE: SUN ONE Studio is almost identical with NetBeans. NetBeans is open source and can be downloaded from www.netbeans.org. I

More information

Moving from CS 61A Scheme to CS 61B Java

Moving from CS 61A Scheme to CS 61B Java Moving from CS 61A Scheme to CS 61B Java Introduction Java is an object-oriented language. This document describes some of the differences between object-oriented programming in Scheme (which we hope you

More information

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

CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O CSE 1223: Introduction to Computer Programming in Java Chapter 7 File I/O 1 Sending Output to a (Text) File import java.util.scanner; import java.io.*; public class TextFileOutputDemo1 public static void

More information

CS 121 Intro to Programming:Java - Lecture 11 Announcements

CS 121 Intro to Programming:Java - Lecture 11 Announcements CS 121 Intro to Programming:Java - Lecture 11 Announcements Next Owl assignment up, due Friday (it s short!) Programming assignment due next Monday morning Preregistration advice: More computing? Take

More information

JAVA ARRAY EXAMPLE PDF

JAVA ARRAY EXAMPLE PDF JAVA ARRAY EXAMPLE PDF Created By: Umar Farooque Khan 1 Java array example for interview pdf Program No: 01 Print Java Array Example using for loop package ptutorial; public class PrintArray { public static

More information

Java CPD (I) Frans Coenen Department of Computer Science

Java CPD (I) Frans Coenen Department of Computer Science Java CPD (I) Frans Coenen Department of Computer Science Content Session 1, 12:45-14:30 (First Java Programme, Inheritance, Arithmetic) Session 2, 14:45-16:45 (Input and Programme Constructs) Materials

More information

While and Do-While Loops. 15-110 Summer 2010 Margaret Reid-Miller

While and Do-While Loops. 15-110 Summer 2010 Margaret Reid-Miller While and Do-While Loops 15-110 Margaret Reid-Miller Loops Within a method, we can alter the flow of control using either conditionals or loops. The loop statements while, do-while, and for allow us execute

More information

CS170 Lab 11 Abstract Data Types & Objects

CS170 Lab 11 Abstract Data Types & Objects CS170 Lab 11 Abstract Data Types & Objects Introduction: Abstract Data Type (ADT) An abstract data type is commonly known as a class of objects An abstract data type in a program is used to represent (the

More information

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

Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6. Handout 3. Strings and String Class. Input/Output with JOptionPane. Handout 3 cs180 - Programming Fundamentals Spring 15 Page 1 of 6 Handout 3 Strings and String Class. Input/Output with JOptionPane. Strings In Java strings are represented with a class type String. Examples:

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: 3.3-3.4 self-check: #16-19 exercises: #11 videos: Ch. 3 #4 Interactive programs We have written programs that print

More information

Programming and Data Structures with Java and JUnit. Rick Mercer

Programming and Data Structures with Java and JUnit. Rick Mercer Programming and Data Structures with Java and JUnit Rick Mercer ii Chapter Title 1 Program Development 2 Java Fundamentals 3 Objects and JUnit 4 Methods 5 Selection (if- else) 6 Repetition (while and for

More information

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

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 Oct 4, 2013, p 1 Name: CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013 1. (max 18) 4. (max 16) 2. (max 12) 5. (max 12) 3. (max 24) 6. (max 18) Total: (max 100)

More information

- User input includes typing on the keyboard, clicking of a mouse, tapping or swiping a touch screen device, etc.

- User input includes typing on the keyboard, clicking of a mouse, tapping or swiping a touch screen device, etc. Java User Input WHAT IS USER INPUT? - Collecting and acting on user input is important in many types of programs or applications. - User input includes typing on the keyboard, clicking of a mouse, tapping

More information

Topic 11 Scanner object, conditional execution

Topic 11 Scanner object, conditional execution Topic 11 Scanner object, conditional execution "There are only two kinds of programming languages: those people always [complain] about and those nobody uses." Bjarne Stroustroup, creator of C++ Copyright

More information

Interactive Applications (CLI) and Math

Interactive Applications (CLI) and Math Interactive Applications (CLI) and Math Interactive Applications Command Line Interfaces The Math Class Example: Solving Quadratic Equations Example: Factoring the Solution Reading for this class: L&L,

More information

LAB4 Making Classes and Objects

LAB4 Making Classes and Objects LAB4 Making Classes and Objects Objective The main objective of this lab is class creation, how its constructer creation, object creation and instantiation of objects. We will use the definition pane to

More information

Arrays. Introduction. Chapter 7

Arrays. Introduction. Chapter 7 CH07 p375-436 1/30/07 1:02 PM Page 375 Chapter 7 Arrays Introduction The sequential nature of files severely limits the number of interesting things you can easily do with them.the algorithms we have examined

More information

Iteration CHAPTER 6. Topic Summary

Iteration CHAPTER 6. Topic Summary CHAPTER 6 Iteration TOPIC OUTLINE 6.1 while Loops 6.2 for Loops 6.3 Nested Loops 6.4 Off-by-1 Errors 6.5 Random Numbers and Simulations 6.6 Loop Invariants (AB only) Topic Summary 6.1 while Loops Many

More information

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be: Lecture 7 Picking Balls From an Urn The problem: An urn has n (n = 10) balls numbered from 0 to 9 A ball is selected at random, its' is number noted, it is set aside, and another ball is selected from

More information

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

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement CompSci 125 Lecture 08 Chapter 5: Conditional Statements Chapter 4: return Statement Homework Update HW3 Due 9/20 HW4 Due 9/27 Exam-1 10/2 Programming Assignment Update p1: Traffic Applet due Sept 21 (Submit

More information

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

Preet raj Core Java and Databases CS4PR. Time Allotted: 3 Hours. Final Exam: Total Possible Points 75 Preet raj Core Java and Databases CS4PR Time Allotted: 3 Hours Final Exam: Total Possible Points 75 Q1. What is difference between overloading and overriding? 10 points a) In overloading, there is a relationship

More information

Interactive Programs and Graphics in Java

Interactive Programs and Graphics in Java Interactive Programs and Graphics in Java Alark Joshi Slide credits: Sami Rollins Announcements Lab 1 is due today Questions/concerns? SVN - Subversion Versioning and revision control system 1. allows

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 01 / 21 / 2014 Instructor: Michael Eckmann Today s Topics Introduction Homework assignment Review the syllabus Review the policies on academic dishonesty and improper

More information

Part 1 Foundations of object orientation

Part 1 Foundations of object orientation OFWJ_C01.QXD 2/3/06 2:14 pm Page 1 Part 1 Foundations of object orientation OFWJ_C01.QXD 2/3/06 2:14 pm Page 2 1 OFWJ_C01.QXD 2/3/06 2:14 pm Page 3 CHAPTER 1 Objects and classes Main concepts discussed

More information

Hands-on Exercise 1: VBA Coding Basics

Hands-on Exercise 1: VBA Coding Basics Hands-on Exercise 1: VBA Coding Basics This exercise introduces the basics of coding in Access VBA. The concepts you will practise in this exercise are essential for successfully completing subsequent

More information

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

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game Directions: In mobile Applications the Control Model View model works to divide the work within an application.

More information

CS 106 Introduction to Computer Science I

CS 106 Introduction to Computer Science I CS 106 Introduction to Computer Science I 01 / 29 / 2014 Instructor: Michael Eckmann Today s Topics Comments and/or Questions? import user input using JOptionPane user input using Scanner psuedocode import

More information

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts

Third AP Edition. Object-Oriented Programming and Data Structures. Maria Litvin. Gary Litvin. Phillips Academy, Andover, Massachusetts Third AP Edition Object-Oriented Programming and Data Structures Maria Litvin Phillips Academy, Andover, Massachusetts Gary Litvin Skylight Software, Inc. Skylight Publishing Andover, Massachusetts Skylight

More information

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION

MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION MIDTERM 1 REVIEW WRITING CODE POSSIBLE SOLUTION 1. Write a loop that computes (No need to write a complete program) 100 1 99 2 98 3 97... 4 3 98 2 99 1 100 Note: this is not the only solution; double sum

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: 3.3-3.4 self-check: #16-19 exercises: #11 videos: Ch. 3 #4 Interactive programs We have written programs that print

More information

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

COSC 1020 3.0 Introduction to Computer Science I Section A, Summer 2005. Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19 Term Test #2 COSC 1020 3.0 Introduction to Computer Science I Section A, Summer 2005 Family Name: Given Name(s): Student Number: Question Out of Mark A Total 16 B-1 7 B-2 4 B-3 4 B-4 4 B Total 19 C-1 4

More information

Java Crash Course Part I

Java Crash Course Part I Java Crash Course Part I School of Business and Economics Institute of Information Systems HU-Berlin WS 2005 Sebastian Kolbe skolbe@wiwi.hu-berlin.de Overview (Short) introduction to the environment Linux

More information

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

First Java Programs. V. Paúl Pauca. CSC 111D Fall, 2015. Department of Computer Science Wake Forest University. Introduction to Computer Science First Java Programs V. Paúl Pauca Department of Computer Science Wake Forest University CSC 111D Fall, 2015 Hello World revisited / 8/23/15 The f i r s t o b l i g a t o r y Java program @author Paul Pauca

More information

Week 2 Practical Objects and Turtles

Week 2 Practical Objects and Turtles Week 2 Practical Objects and Turtles Aims and Objectives Your aim in this practical is: to practise the creation and use of objects in Java By the end of this practical you should be able to: create objects

More information

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

J a v a Quiz (Unit 3, Test 0 Practice) Computer Science S-111a: Intensive Introduction to Computer Science Using Java Handout #11 Your Name Teaching Fellow J a v a Quiz (Unit 3, Test 0 Practice) Multiple-choice questions are worth 2 points

More information

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

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders 1.00 Lecture 1 Course Overview Introduction to Java Reading for next time: Big Java: 1.1-1.7 Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

More information

Programming Languages CIS 443

Programming Languages CIS 443 Course Objectives Programming Languages CIS 443 0.1 Lexical analysis Syntax Semantics Functional programming Variable lifetime and scoping Parameter passing Object-oriented programming Continuations Exception

More information

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

CS1020 Data Structures and Algorithms I Lecture Note #1. Introduction to Java CS1020 Data Structures and Algorithms I Lecture Note #1 Introduction to Java Objectives Java Basic Java features C Java Translate C programs in CS1010 into Java programs 2 References Chapter 1 Section

More information

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro

Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro Lab 9 Access PreLab Copy the prelab folder, Lab09 PreLab9_Access_intro, to your M: drive. To do the second part of the prelab, you will need to have available a database from that folder. Creating a new

More information

Chapter 2 Elementary Programming

Chapter 2 Elementary Programming Chapter 2 Elementary Programming 2.1 Introduction You will learn elementary programming using Java primitive data types and related subjects, such as variables, constants, operators, expressions, and input

More information

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor Vim, Emacs, and JUnit Testing Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor Overview Vim and Emacs are the two code editors available within the Dijkstra environment. While both

More information

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives Introduction to Programming and Algorithms Module 1 CS 146 Sam Houston State University Dr. Tim McGuire Module Objectives To understand: the necessity of programming, differences between hardware and software,

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 4 Lecture 4-1: Scanner; if/else reading: 3.3 3.4, 4.1 Interactive Programs with Scanner reading: 3.3-3.4 1 Interactive programs We have written programs that print console

More information

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

Topics. Parts of a Java Program. Topics (2) CS 146. Introduction To Computers And Java Chapter Objectives To understand: Introduction to Programming and Algorithms Module 2 CS 146 Sam Houston State University Dr. Tim McGuire Introduction To Computers And Java Chapter Objectives To understand: the meaning and placement of

More information

How can I keep track of the money in my checking account?

How can I keep track of the money in my checking account? Keeping Track of Your Money 3 MONEY MATTERS The BIG Idea How can I keep track of the money in my checking account? AGENDA Approx. 45 minutes I. Warm Up: Where Did the Money Go? (10 minutes) II. How To

More information

Programmierpraktikum

Programmierpraktikum Programmierpraktikum Claudius Gros, SS2012 Institut für theoretische Physik Goethe-University Frankfurt a.m. 1 of 21 10/16/2012 09:29 AM Java - A First Glance 2 of 21 10/16/2012 09:29 AM programming languages

More information

Arrays in Java. Working with Arrays

Arrays in Java. Working with Arrays Arrays in Java So far we have talked about variables as a storage location for a single value of a particular data type. We can also define a variable in such a way that it can store multiple values. Such

More information

Add an Audit Trail to your Access Database

Add an Audit Trail to your Access Database Add an to your Access Database Published: 22 April 2013 Author: Martin Green Screenshots: Access 2010, Windows 7 For Access Versions: 2003, 2007, 2010 About This Tutorial My Access tutorials are designed

More information

13 File Output and Input

13 File Output and Input SCIENTIFIC PROGRAMMING -1 13 File Output and Input 13.1 Introduction To make programs really useful we have to be able to input and output data in large machinereadable amounts, in particular we have to

More information

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

Programming in Java. 2013 Course Technology, a part of Cengage Learning. C7934_chapter_java.qxd 12/20/11 12:31 PM Page 1 Programming in Java Online module to accompany Invitation to Computer Science, 6th Edition ISBN-10: 1133190820; ISBN-13: 9781133190820 (Cengage Learning,

More information

The following program is aiming to extract from a simple text file an analysis of the content such as:

The following program is aiming to extract from a simple text file an analysis of the content such as: Text Analyser Aim The following program is aiming to extract from a simple text file an analysis of the content such as: Number of printable characters Number of white spaces Number of vowels Number of

More information

Building Java Programs

Building Java Programs Building Java Programs Chapter 5 Lecture 5-2: Random Numbers reading: 5.1-5.2 self-check: #8-17 exercises: #3-6, 10, 12 videos: Ch. 5 #1-2 1 The Random class A Random object generates pseudo-random* numbers.

More information

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods.

WHAT ARE PACKAGES? A package is a collection of related classes. This is similar to the notion that a class is a collection of related methods. Java Packages KNOWLEDGE GOALS Understand what a package does. Organizes large collections of Java classes Provides access control for variables and methods using the modifier 'protected' Helps manage large

More information

Chapter 2 Introduction to Java programming

Chapter 2 Introduction to Java programming Chapter 2 Introduction to Java programming 1 Keywords boolean if interface class true char else package volatile false byte final switch while throws float private case return native void protected break

More information

Creating a Java application using Perfect Developer and the Java Develo...

Creating a Java application using Perfect Developer and the Java Develo... 1 of 10 15/02/2010 17:41 Creating a Java application using Perfect Developer and the Java Development Kit Introduction Perfect Developer has the facility to execute pre- and post-build steps whenever the

More information

More on Objects and Classes

More on Objects and Classes Software and Programming I More on Objects and Classes Roman Kontchakov Birkbeck, University of London Outline Object References Class Variables and Methods Packages Testing a Class Discovering Classes

More information

DEPARTMENT OF EDUCATION. Online Application General Information

DEPARTMENT OF EDUCATION. Online Application General Information DEPARTMENT OF EDUCATION CHILD NUTRITION PROGRAM Online Application General Information Contents Revision History... 2 Revision History Chart... 2 Welcome to the Child Nutrition Programs Website... 3 What

More information

Visual Logic Instructions and Assignments

Visual Logic Instructions and Assignments Visual Logic Instructions and Assignments Visual Logic can be installed from the CD that accompanies our textbook. It is a nifty tool for creating program flowcharts, but that is only half of the story.

More information

Make Money Like Banks Do The How to make money with credit cards report

Make Money Like Banks Do The How to make money with credit cards report Make Money Like Banks Do The How to make money with credit cards report (Please make sure to read the Frequently Asked Questions at the bottom for further clarification after reading the report.) Banks

More information

Chapter 5. Microsoft Access

Chapter 5. Microsoft Access Chapter 5 Microsoft Access Topic Introduction to DBMS Microsoft Access Getting Started Creating Database File Database Window Table Queries Form Report Introduction A set of programs designed to organize,

More information

Understanding class definitions

Understanding class definitions OFWJ_C02.QXD 2/3/06 2:28 pm Page 17 CHAPTER 2 Understanding class definitions Main concepts discussed in this chapter: fields methods (accessor, mutator) constructors assignment and conditional statement

More information

Manual For Using the NetBeans IDE

Manual For Using the NetBeans IDE 1 Manual For Using the NetBeans IDE The content of this document is designed to help you to understand and to use the NetBeans IDE for your Java programming assignments. This is only an introductory presentation,

More information

Chapter 2 Basics of Scanning and Conventional Programming in Java

Chapter 2 Basics of Scanning and Conventional Programming in Java Chapter 2 Basics of Scanning and Conventional Programming in Java In this chapter, we will introduce you to an initial set of Java features, the equivalent of which you should have seen in your CS-1 class;

More information

Budget Main Window (Single Bank Account) Budget Main Window (Multiple Bank Accounts)

Budget Main Window (Single Bank Account) Budget Main Window (Multiple Bank Accounts) Budget Main Window (Single Bank Account) Budget Main Window (Multiple Bank Accounts) Page 1 of 136 Using Budget Help Budget has extensive help features. To get help use Budget's Help > Budget Help menu

More information

The single biggest mistake many people make when starting a business is they'll create a product...

The single biggest mistake many people make when starting a business is they'll create a product... Killer Keyword Strategies - Day 1 "A Guaranteed Way To Find A Starving Crowd Using The Power Of Keyword Research..." The single biggest mistake many people make when starting a business is they'll create

More information

MANUAL USER GUIDE FOR EMR PRIMARY HEALTH CARE SYSTEM

MANUAL USER GUIDE FOR EMR PRIMARY HEALTH CARE SYSTEM MANUAL USER GUIDE FOR EMR PRIMARY HEALTH CARE SYSTEM By Faustin GASHAYIJA Version 1.0 1 Table of contents Contents Table of contents... 2 Browser... 4 Log in openmrs website... 4 OpenMRS welcome page...

More information