Introduction to Programming

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 3: Flow Control I: For Loops.
Introduction to Programming Java Lab 1: My First Program 11 January JavaLab1.ppt Ping Brennan
Introduction to Programming Overview of Week 2 25 January Ping Brennan
SOFTWARE AND PROGRAMMING 1 Lecture 3: Ticketing machine: Constructor, method, menu Instructor: Prof. Boris Mirkin web-site
Introduction to Programming Java Lab 7: Loops 22 February JavaLab7 lecture slides.ppt Ping Brennan
22 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
18 January 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
1 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming Java Lab 4: Formatted Output and Strings 1 February JavaLab4 lecture slides.ppt Ping Brennan
8 March 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
Introduction to Programming
Introduction to Programming Java Lab 2: Variables and Number Types 1 JavaLab2 lecture slides.ppt Ping Brennan
8 February 2013Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Introduction to Programming Java Lab 5: Boolean Operations 8 February JavaLab5 lecture slides.ppt Ping Brennan
Introduction to Programming Java Lab 3: Variables and Number types 25 January JavaLab3.ppt Ping Brennan
Introduction to Programming
Lecture 10 Methods COMP1681 / SE15 Introduction to Programming.
Programming Methodology (1). public class Hello { public static void main(String[] args) { System.out.println("Hello world"); } } Hello world.
Chapter 8: Arrays.
1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Programming Methodology (1). Implementing Methods main.
1 Arrays An array is a special kind of object that is used to store a collection of data. The data stored in an array must all be of the same type, whether.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Computer Programming Lab(7).
Building Java Programs Interactive Programs w/ Scanner What is a class? What is an object? What is the difference between primitive and object variables?
Lecture 8 Instructor: Craig Duckett. Assignments TONIGHT Lecture 8 Assignment 2 Due TONIGHT Lecture 8 by midnight Monday, February 2 nd Lecture 10 Assignment.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
CS110 Programming Language I
Computer and Programming
Computer Programming Lab 8.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Introduction to Information and Computer Science Computer Programming Lecture c This material (Comp4_Unit5c), was developed by Oregon Health and Science.
Java Fundamentals 3 Input and Output statements. Standard Output Window Using System.out, we can output multiple lines of text to the standard output.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
Can we talk?. In Hello World we already saw how to do Standard Output. You simply use the command line System.out.println(“text”); There are different.
COM S 207 Method Instructor: Ying Cai Department of Computer Science Iowa State University
CHAPTER 5 GC 101 Input & Output 1. INTERACTIVE PROGRAMS  We have written programs that print console output, but it is also possible to read input from.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
Methods. Methods also known as functions or procedures. Methods are a way of capturing a sequence of computational steps into a reusable unit. Methods.
Java methods Methods break down large problems into smaller ones Your program may call the same method many times saves writing and maintaining same code.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
Component 4: Introduction to Information and Computer Science Unit 5: Overview of Programming Languages, Including Basic Programming Concepts Lecture 3.
Building java programs, chapter 3 Parameters, Methods and Objects.
Martin T. Press.  Main Method and Class Name  Printing To Screen  Scanner.
CPSC 233 Tutorial 5 February 9 th /10 th, Java Classes Each Java class contains a set of instance variables and methods Instance Variables: Type.
import java.util.Scanner; class myCode { public static void main(String[] args) { Scanner input= new Scanner(System.in); int num1; System.out.println(“Enter.
Computer Programming 12 Mr. Jean March 5 th, 2014.
4 March 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems
CS 115 OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS 1 Copyright: 2015 Illinois Institute of Technology_ George Koutsogiannakis.
Building Java Programs Chapter 4 Lecture 4-1: Scanner ; cumulative algorithms reading: 3.3 – 3.4, 4.2.
Copyright 2008 by Pearson Education Building Java Programs Chapter 3 Lecture 3-3: Interactive Programs w/ Scanner reading: self-check: #16-19.
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
CSC111 Quick Revision.
Introduction to programming in java
Introduction to Programming
Department of Computer Science
Something about Java Introduction to Problem Solving and Programming 1.
INPUT STATEMENTS GC 201.
Introduction to Classes and Methods
Introduction to Programming
OBJECT ORIENTED PROGRAMMING I LECTURE 11 GEORGE KOUTSOGIANNAKIS
Scope of variables class scopeofvars {
Building Java Programs
Introduction to Programming
Building Java Programs
Presentation transcript:

Introduction to Programming Java Lab 8: Methods JavaLab8 lecture slides.ppt Ping Brennan (p.brennan@dcs.bbk.ac.uk) 1 March 2013

Java Project Project Name: JavaLab8 RepeatString ReadDouble

Method Declaration for cubeVolume (in JFE, part 5.2) Passing a parameter Type of return value Type of parameter variable Method name Name of parameter variable public static double cubeVolume(double sideLength) { double volume = sideLength * sideLength * sideLength; return volume; } Method body, executed when method is called. return statement exits method and returns volume.

Class RepeatString Class RepeatString contains two methods: Objectives First method is the usual one, main Second method repeat returns the string str repeated n times. (See JFE, P5.5) Objectives Understand how to pass and return values in methods. Using methods to solve problems.

Class RepeatString Method public static String repeat(String str, int n) Input int n = 3; // first example int n = 1; // second example Examples (in main): String str1 = repeat("ho", 3); String str2 = repeat("ho", 1); Loop for statement Print str1 repeated 3 times. Print str2 repeated 1 time. Output hohoho ho

Anatomy of Class RepeatString import java.util.Scanner; public class RepeatString { public static void main(String[] args) String str1 = repeat("ho", 3); System.out.println("First example: " + str1); /* To do: write more code to make a number of calls to the method repeat */ } // end of main /* To do: insert code for the method repeat here (which is shown on next slide) */ } // end of class RepeatString Method name Call to method repeat

Method Declaration for repeat Passing a parameter Type of return value Type of parameter variable Method name Name of parameter variable public static String repeat(String str, int n) { String result = ""; /* To do: write a for statement to loop n times, each time joining the string, str, to current value in result. */ return result; } Method body, executed when method is called. return statement exits method and returns result.

Class ReadDouble Displays a prompt string, followed by a space, and then reads in a number of type double. Objective Construct a method to return a floating point number after reading it inside the method when prompted.

Class ReadDouble Method public static double readDouble(String prompt) Usage in method main double salary = readDouble("Please enter your salary: " ); // read in salary Input A prompt string. double salary; Example (in main): salary = readDouble("Please enter your salary: "); // Method displays prompt, then // reads in a floating point number, // 31000.50, and returns it. Computations Inside method readDouble: double inSalary = in.nextDouble(); return inSalary; Prompt Please enter your salary: 31000.50 (user’s input in red) Output The entered salary is 31000.50

Anatomy of Class ReadDouble import java.util.Scanner; public class ReadDouble { public static void main(String[] args) double salary = readDouble("Please enter your salary: "); /* To do: write code to print out the salary */ /* To do: include a number of calls to the method readDouble, and print the results */ } // end of main /* To do: insert code for the method readDouble here (which is shown on next slide) */ } // end of class ReadDouble

Method declaration for readDouble public static double readDouble(String prompt) { Scanner in = new Scanner(System.in); System.out.print(prompt + " "); /* To do: write code to declare a variable, inSalary, of type double, and assign it the keyboard input. Hint: use in.nextDouble() to read in a value. */ /* To do: lastly write code to return inSalary */ } // end of readDouble