Practice + Method Xiaozhong Liu

Slides:



Advertisements
Similar presentations
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Advertisements

Constructors & An Introduction to Methods. Defining Constructor – Car Example Public class car { String Model; double speed; String colour; { Public Car.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Week 9: Methods 1.  We have written lots of code so far  It has all been inside of the main() method  What about a big program?  The main() method.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Lecture 3: Topics If-then-else Operator precedence While loops Static methods Recursion.
Wednesday, 12/11/02, Slide #1 CS 106 Intro to Comp. Sci. 1 Wednesday, 12/11/02  QUESTIONS??  Today: CLOSING CEREMONIES!  HW #5 – Back Monday (12/16)
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
 To be able to write larger programs ◦ By breaking them down into smaller parts and passing data between the parts.  To understand the concepts of Methods.
COMP More About Classes Yi Hong May 22, 2015.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
Methods and You. Up to this point, I have covered many different data types with you. Variables can be considered the nouns of an English sentence. If.
Introduction to Application Programming IST 256 Application Programming for Information Systems Xiaozhong Liu
Week 6 - Wednesday.  What did we talk about last time?  Exam 1 post-mortem  Recursive running time.
Passing Other Objects Strings are called immutable which means that once a String object stores a value, it never changes –recall when we passed a message.
Week 2 - Wednesday.  What did we talk about last time?  Data representation  Binary numbers  Types  int  boolean  double  char  String.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Methods (a.k.a. Functions)
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
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.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 Chapter 6 Methods. 2 Motivation Find the sum of integers from 1 to 10, from 20 to 30, and from 35 to 45, respectively.
User Defined Methods Methods are used to divide complicated programs into manageable pieces. There are predefined methods (methods that are already provided.
Java Methods 11/10/2015. Learning Objectives  Be able to read a program that uses methods.  Be able to write a write a program that uses methods.
Chapter 5 : Methods Part 2. Returning a Value from a Method  Data can be passed into a method by way of the parameter variables. Data may also be returned.
Classes - Intermediate
OOP Basics Classes & Methods (c) IDMS/SQL News
COP 2220 Computer Science I Topics –Breaking Problems Down –Functions –User-defined Functions –Calling Functions –Variable Scope Lecture 4.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Java 5 Class Anatomy. User Defined Classes To this point we’ve been using classes that have been defined in the Java standard class library. Creating.
Comp1004: Building Better Objects I Methods. Coming up Methods and Parameters – Why Parameterise? – Call by value, call by reference Return Types – Methods.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Methods Matthew Harrison. Overview ● There are five main aspects of methods... ● 1) Modifiers – public, private ● 2) Method Name ● 3) Parameters ● 4)
Functions + Overloading + Scope
Chapter 2 Clarifications
Suppose we want to print out the word MISSISSIPPI in big letters.
Department of Computer Science
Sum of natural numbers class SumOfNaturalNumbers {
Lecture 2: Data Types, Variables, Operators, and Expressions
Primitive Data, Variables, Loops (Maybe)
Advanced Programming in Java
Java Methods Making Subprograms.
Method Mark and Lyubo.
Starting Out with Java: From Control Structures through Objects
Computing Adjusted Quiz Total Score
Building Java Programs
Java Methods Making Subprograms.
Chapter 4 Topics: class declarations method declarations
Chapter 6 Methods.
AKA the birth, life, and death of variables.
Java Methods Making Subprograms.
Methods and Data Passing
class PrintOnetoTen { public static void main(String args[]) {
BBIT 212/ CISY 111 Object Oriented Programming (OOP)
Building Java Programs
Scope of variables class scopeofvars {
Building Java Programs
Building Java Programs
CSS161: Fundamentals of Computing
Building Java Programs
Building Java Programs
Methods/Functions.
Building Java Programs
Methods and Data Passing
Presentation transcript:

Practice + Method Xiaozhong Liu http://scholarwiki.indiana.edu/S517/S517.html Web Programming Practice + Method Xiaozhong Liu

Read the following code: double number; String result; If (number >= 5) { result = “good” } else { result = “bad” number = 5.0; System.out.println(result);

Read the following code: double salary, taxrate, income; salary = 1893.20 if (salary >= 3000) { taxrate = 0.30; } else { if (salary >= 2000) { taxrate = 0.20; } else { taxrate = 0.10; income = salary * (1 - taxrate); System.out.println(income);

Read the following code: double hours, rate, salary; String position = “manager”; hours = 40; If (position.equals(“manager”) || hours > 40) { rate = 80; } else { If (position.equals(“employee”) || hours > 40) { rate = 60; rate = 40; } salary = hours * rate

Read the following code: int result, step; step = 1; result = 20; While (result > 10) { result = result – step; if (step%2 == 0) { step = step - 1; } else { step = step + 1; System.out.println(result); 1: step = 1; result = 19 2: step = 3; result = 16 3: step = 5; result = 11 4: step = 7; result = 4

for (int index = 1; index < 17; index++) { total = total + 3; } Read the following code: int total = 0; for (int index = 1; index < 17; index++) { total = total + 3; } System.out.println(total); 1: step = 1; result = 19 2: step = 3; result = 16 3: step = 5; result = 11 4: step = 7; result = 4

Read the following code: Suppose IUB wants to expand student number 1.5% every year. Input the “current student number” and “years”; predict the number of students in the future (years number later).

What is the ideal programming style? public static void main(String[] args) { String query = “……”; if (spellcorrect(query)) { results = search(query); results = rank(results); show(results); } else { query = suggestQuery(query); updateQuery(query);

Procedure public static void main(String[] args) { int number; … ABC (number) } Private static void ABC (int targetnum) { … }

Concept of Procedures and Functions All modern programming languages have a way to break the program into smaller pieces by placing a piece of code in a procedure or function This type of modularization is good programming design Often entire sections of code need to be repeated in different parts of the program, for example sorting a list of data, and making it into a procedure streamlines the process Write once, use many times Java calls these methods

Method definition public static int squareR ( int y) { scope keywords return type method name formal parameter list method header public static int squareR ( int y) { // variable to hold the square of y int ysquared; ysquared = y * y; return ysquared; } method body

Procedure – organize your code public static void main(String[] args) { String query = “???”; String results; …… results = search (query); results = rank (results); show_results(results); … } private static String search (String query) { String results; … return results; }

Wine or water? public static void main(String[] args) { String drink; int age = ??; if (oldenough(age)) { drink = “wine”; } else { drink = “water”; oldenough(age) Returns a boolean result

1+2+3+4+5…100 public static void main(String[] args) { int result = 0; int start = 1; int end = 100; result = sumnums (start, end); System.out.println(result); } sumnums (start, end) Returns int

Convert temperature public static void main(String[] args) { double Fahrenheit, Celsius = ??; Fahrenheit = convert_temp (Celsius ); System.out.println(Fahrenheit ); } convert_temp (Celsius ) Returns double

public class Example { public double square (double x) { x *= x; return x; } public static void main(String[ ] args) { double x = 5.0; double y = square (x); System.out.println (x + " " + y);

Overload public int add (int x, int y) { ... } public int add (int x, int y, int z) { … } public int add (double x, double y) { ... } same method name but different numbers or types of parameters!! public int add (int x, int y) { ... } public int add (int k1, int k2) { … } WRONG!

Recursive method public class test { public static int changenumber (int number) { if (number < 0) { return number; } else { number = number - 5; return changenumber (number); public static void main(String[] args) { int x = changenumber (16); System.out.println(x);

Practice 2 + 4 + 6 + 8 … 1000 3 + 6 + 9 + 12 … 1500 public static int sumnum (int startnum, int endnum, int intervalnum) { int number = startnum; int result = 0; while (number < endnum) { result = result + number; number = number + intervalnum; } return result;

Your S517 course score? Lab – 15%; Assignment – 45%; two exams – 40% 90-100: A; 75 – 89: B; 60 – 74: C; 45 – 60 D; 0 – 45 T.T