Download presentation
Presentation is loading. Please wait.
1
Practice + Method Xiaozhong Liu
Web Programming Practice + Method Xiaozhong Liu
2
Read the following code:
double number; String result; If (number >= 5) { result = “good” } else { result = “bad” number = 5.0; System.out.println(result);
3
Read the following code:
double salary, taxrate, income; salary = 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);
4
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
5
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
6
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
7
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).
8
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);
9
Procedure public static void main(String[] args) { int number; …
ABC (number) } Private static void ABC (int targetnum) { … }
10
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
11
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
12
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; }
13
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
14
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
15
Convert temperature public static void main(String[] args) {
double Fahrenheit, Celsius = ??; Fahrenheit = convert_temp (Celsius ); System.out.println(Fahrenheit ); } convert_temp (Celsius ) Returns double
16
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);
17
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!
18
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);
19
Practice … 1000 … 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;
20
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.