Presentation is loading. Please wait.

Presentation is loading. Please wait.

Classes, methods, and conditional statements We’re past the basics. These are the roots.

Similar presentations


Presentation on theme: "Classes, methods, and conditional statements We’re past the basics. These are the roots."— Presentation transcript:

1 Classes, methods, and conditional statements We’re past the basics. These are the roots.

2 What is a class? A class is an object variable. Classes can be divided into subclasses that inherit some properties of the superclass. Built into the Java language are a variety of classes that contain useful functions and calculating tools. We’ll work more with classes later after we learn about methods.

3 The main class In Java, the main class is the actual program itself. It is executable. import extra.*; public class Graduation { public static void main(String args[]) { Std.out.println( “This is a very simple program.” ); } public – can be accessed by all parts of the program class – signifies that the code between the braces { } belongs to a class Graduation – gives the class a name, always begins with a capital letter.

4 What is a method? A method is a smaller block of code designated to do something. It usually takes input and gives output, but sometimes it doesn’t need one or the other (or either). A method is like a mini-program that can be run over and over. Methods are sometimes called “subroutines” or “functions.”

5 The main method In Java, the main method is where the computer begins to read code when the program runs. import extra.*; public class Graduation { public static void main(String args[]) { Std.out.println( “This is a very simple program.” ); } public – can be accessed by all parts of the program static – the function is permanent void – the function gives no output to the computer itself main – the function is the program’s main function String args[] – we are coding using text

6 Methods in action Write a program to do the following: The user will input two double variables, a and b, representing the lengths of the arms of a right triangle The program must find the value of the hypotenuse squared, that is, a 2 +b 2 How could we write a program to do this?

7 A solution import extra.*; public class Graduation { public static void main(String args[]) { double a; double b; double cSquared Std.out.println( “Please input a.” ); a = Std.in.readDouble(); Std.out.println( “Please input b.” ); b = Std.in.readDouble(); cSquared = Math.pow(a,2) + Math.pow(b,2) Std.out.println( “c^2 equals” + cSquared); }

8 A better solution: use a method Whenever there is an operation that we perform repeatedly, it is helpful to place it into its own method.

9 How to write a method Here’s the structure of a method: modifier resulttype name (inputs) { calculations; return; } Here’s what our method for squaring might look like: private double square (double d) { double dSquared = Math.pow(d^2); return dSquared; } private because only our class needs to be able to use it double because the return (or output) is a double

10 Implementing a method Place the code for the method itself inside the class, either before or after the main method To call a method from the main class, write the method’s name, followed by the method’s argument (input) in parens. Example: double aSquared = square(a);

11 A better solution: the code import extra.*; public class Graduation { private double square (double d) { double dSquared = Math.pow(d^2); return dSquared; } public static void main(String args[]) { double a; double b; double cSquared Std.out.println( “Please input a.” ); a = Std.in.readDouble(); Std.out.println( “Please input b.” ); b = Std.in.readDouble(); cSquared = square(a) + square(b); Std.out.println( “c^2 equals” + cSquared); }

12 Your turn: Write a method (not a program) that takes an integer as its argument (input), halves the integer, and returns (outputs) a double as the answer. Your method should be private.

13 A solution private double half (int i) { double halfOfI = i / 2.0 ; return halfOfI; } Notice: we divided an integer by a double (i/2.0). If we had divided an integer by an integer (i/2), the resulting integer would be truncated, and so would the double. Remember: the argument is input, the return is output

14 Conditional statements! Conditional statements are the heart of programming, they allow the program to react differently to different inputs

15 The while statement Suppose we want to make a program that counts to ten. We want our program to start counting at one, but we want it to know to stop counting, too! How do we get it to stop counting at ten?

16 Writing a while statmenet int counter = 1; while ( counter <= 10 ) { Std.out.println( counter ); counter++; }

17 Mechanics of a while statement If the condition inside the parens () is TRUE, the code inside the braces {} is executed. If the condition inside the parens () is FALSE, then the code inside the braces {} is NOT executed. int counter = 1; while ( counter <= 10 ) { Std.out.println( counter ); counter++; }

18 The if statement Revist the Graduation program that calculated the years left you had in high school. It would be prudent to put in code that prevents the input of a number greater than 12.

19 Writing an if statmenet The last line in our code was this: lastGrade = Std.in.readInt(); Here’s what a good if statement would look like: if ( lastGrade > 12 ) { Std.out.println(“You’ve already graduated.”); } else { int yearsLeft; yearsLeft = 12 - lastGrade;... }

20 Mechanics of an if statement If the condition inside the parens () is TRUE, the code inside the braces {} is executed. If the condition inside the parens () is FALSE, then the code inside the braces {} is NOT executed. Instead, the code inside the braces after else is executed. (The else and its braces are optional.) if ( lastGrade > 12 ) { Std.out.println(“You’ve already graduated.”); } else { int yearsLeft; yearsLeft = 12 - lastGrade;... }

21 The for loop Suppose we want to make a program that prints the squares of the first ten integers We could use a while loop, but we could also use a for loop…

22 Writing a for loop int counter; for ( counter = 1; counter <=10; counter++ ) { Std.out.println( (counter * counter) ); }

23 Mechanics of a for loop There are THREE statements in the parens(), and there are semicolons after the first two. The FIRST statement is executed the first time through the loop, and the THIRD statement is executed at the beginning of each subsequent trip through the loop. If the SECOND is true, then the code inside the braces {} is executed. If it is FALSE, then the code inside the braces {} is NOT executed. int counter; for ( counter = 1; counter <=10; counter++ ) { Std.out.println( (counter * counter) ); }

24 It’s your choice! while loop: int counter = 1; while ( counter <= 10 ) { Std.out.println( ( counter * counter ) ); counter++; } for loop: int counter; for ( counter = 1; counter <=10; counter++ ) { Std.out.println( (counter * counter) ); }

25 Combining methods and conditional statements! Write a program that asks a user for two integers: a base and an exponent. The program should check to make sure that the exponent is at least 1. Then, the program should use a method (not Math.pow) to do the multiplying. Some kind of loop should be used.


Download ppt "Classes, methods, and conditional statements We’re past the basics. These are the roots."

Similar presentations


Ads by Google