Title: Classes, methods, and conditional statements
1Classes, methods, and conditional statements
- Were past the basics. These are the roots.
2What 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. - Well work more with classes later after we learn
about methods.
3The 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.
4What 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 doesnt 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.
5The 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 programs main
function String args we are coding using text
6Methods 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, a2b2 - How could we write a program to do this?
7A 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.o
ut.println( c2 equals cSquared)
8A better solution use a method
- Whenever there is an operation that we perform
repeatedly, it is helpful to place it into its
own method.
9How to write a method
- Heres the structure of a methodmodifier
resulttype name (inputs) calculations return
- Heres what our method for squaring might look
likeprivate double square (double d) double
dSquared Math.pow(d2) return dSquared
private because only our class needs to be able
to use it double because the return (or output)
is a double
10Implementing 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
methods name, followed by the methods argument
(input) in parens. - Exampledouble aSquared square(a)
11A better solution the code
- import extra.
- public class Graduation
- private double square (double d) double
dSquared Math.pow(d2) 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)
12Your 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.
13A 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
14Conditional statements!
- Conditional statements are the heart of
programming, they allow the program to react
differently to different inputs
15The 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?
16Writing a while statmenet
- int counter 1
- while ( counter lt 10 )
- Std.out.println( counter )
- counter
17Mechanics 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 lt 10 )
- Std.out.println( counter )
- counter
-
18The 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.
19Writing an if statmenet
- The last line in our code was this
- lastGrade Std.in.readInt()
- Heres what a good if statement would look like
- if ( lastGrade gt 12 )
- Std.out.println(Youve already graduated.)
- else int yearsLeft
- yearsLeft 12 - lastGrade
- ...
-
20Mechanics 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 gt 12 )
- Std.out.println(Youve already graduated.)
- else int yearsLeft
- yearsLeft 12 - lastGrade
- ...
-
21The 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
22Writing a for loop
- int counter
- for ( counter 1 counter lt10 counter )
- Std.out.println( (counter counter) )
23Mechanics 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 lt10 counter )
- Std.out.println( (counter counter) )
-
24Its your choice!
- while loop
- int counter 1
- while ( counter lt 10 )
- Std.out.println( ( counter counter ) )
- counter
-
- for loop
- int counter
- for ( counter 1 counter lt10 counter )
- Std.out.println( (counter counter) )
25Combining 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.