04-Review of Programming 1

Download Report

Transcript 04-Review of Programming 1

Programming 2
Review of Programming 1
• To create a Java console application, we need the
set of code:
public class TrivialApplication
{
public static void main(String args[])
//main starts
{
<At first, everything will go here>
}
//main ends
}
• All of your code at first will go between the brackets of main. Do
not change anything in any of the names for a while. Keep all your
programs the same.
• You do not need to import any files yet, the file java.lang is
automatically included.
• Java is case sensitive,
– The Class is different from class is different
from CLASS, etc.
– The computer does not understand when the
wrong word is used
• Java has free format,
– You can use any number of spaces and line
breaks to separate words
– Good taste dictates that you lay out your
program in a readable fashion.
Java Variable Types:
•Integers:
int number;
•Decimals:
double numEntered;
•Strings:
String word;
•boolean:
boolean value; - can store true or false
char is a valid type, but you can only store one character.
Strings are better since they can store nearly unlimited
characters
Java Identifiers ( Variable, Object and Class Names ):
•Can contain numbers, letters, or the underscore
•Must only begin with letters, the underscore or the dollar sign ($)
Examples
• Good
team , number1 , _beginningBalence , c3pO,
$price, ….
• Bad
5peas, !num , waytogo , (integer, …
• BE DESCRIPTIVE!!!!!!!!!!!
Declaration of Variables
• Declaration is the creation of the variable in
main memory.
• Begin with the type of the variable, a space,
then the name.
int number;
double userEnteredRadius;
String name;
Initializing Variables
• Initialization is giving the variable a starting
value.
int number = 7;
String name = “Hello”;
Console Output for Java
• Standard output is done with the print() or println() commands
– Defined in java.lang, but you do not need to import
– prints to the screen
– used with the System.out object
System.out.println(“Hello World”);
• print() stays on the same line, println() goes to the next line.
• Can use the escape character “\n”
Code:
Output:
System.out.print( “Hello ”);
System.out.print( “World”);
Hello World
System.out.println(“Hello”);
System.out.print(“World”);
Hello
World
System.out.print(“Hello\nWorld”);
Hello
World
int num = 2;
System.out.print(“Num is “ + num);
Num is 2
Escape Characters
\n
Newline - Position the screen cursor to the beginning
of the next line
\t
Horizontal Tab - Move the screen cursor to the next
tab spot
\r
Carriage Return - Position the screen cursor to the
beginning of the current line. Do not advance to the current
line. Any characters output after the carriage return overwrite
the characters previously output on that line.
\”
Double quote. Used to print quotes to the screen.
System.out.println( “ \”in quotes\” ”);
displays
“in quotes”
Assignment Statements
• Always begin with where you store the data
followed by an equal sign ( = , assignment
operator )
number = 7;
cash = cash + 3;
Java Mathematical Operators:
•Addition:
+
•Subtraction:
-
•Multiplication:
*
•Division:
/
•Modulus
%
•Assignment:
=
Mathematical Operators
• Order of Precedence:
– Grouping symbols
( )
Evaluated from inside - out
– Mult, Mod, Divide
*,%,/
Evaluated from left to right
– Addition, subtraction
+,Evaluated from left to right
Compound Assignment
Operators
•
•
•
•
•
•
•
+=
-=
*=
/=
%=
++
--
x+=y; same as x = x + y;
x-=y; same as x = x - y;
x*=y; same as x = x * y;
x/=y; same as x = x / y;
x%=y; same as x = x % y;
x++ or ++x same as x = x + 1;
x-- or --x same as x = x - 1;
Boolean Expressions
• A boolean expression is a statement that can
be evaluated to either true( 1 ) or false( 0 )
5==3
number > 5
Equality Operators
• ==
x == y
x is equal to y
• !=
x != y
x is not equal to y
Relational Operators
• >
x>y
x is greater than y
• <
x<y
x is less than y
• >=
x >= y
x is greater than or equal to y
• <=
x <= y
x is less than or equal to y
Putting multiple Boolean
Expressions Together
• Use of the && ( and ) operator
• Use of the | | ( or ) operator
(4.2 > = 5.0 ) && ( 8 = = ( 3 + 5 ))
The above statement would be false
Order of Precedence
()
!
*,/,%
+,<, <= , > , >= , = = , != ( Careful on = or = =)
&&
||
If Statements
int sum = 0, num = -7;
if ( num > 0 )
sum = sum + num;
System.out.println( sum );
If Statements( continued)
• Remember brackets if more than one
statement is part of the if
sum = 0;
num = 10
if ( num > 0.0 )
{
sum = sum + num;
num++;
}
System.out.println( sum );
If-else Statements
if( num > 0 )
negCount = negCount + 1;
else
nonNegCount = nonNegCount + 1;
• Same rules for brackets
• Never have an if - else if statement
If - else if - else Statements
if( num = = 1)
System.out.println( “Got a 1” );
else if (num = = 2)
System.out.println( “Got a 2” );
else if( num = = 3 )
System.out.println( “Got a 3” );
else
System.out.println( “Got something else” );
Switch Statements
switch( age )
{
case 18:
System.out.println( “18” );
break;
case 19:
System.out.println( “19” );
break;
default:
System.out.println( “Not 18 or 19” );
}
For loops
for ( int x = 1; x < = 10; x++ )
System.out.println( x );
• Use brackets if more than one statement
While loops
a=1;
while ( a < 50 )
{
System.out.println( a );
a*=3;
}
Do While Loops
j=0;
do
{
j = j + 2;
System.out.println( j );
}while( j !=5 );
Methods
• The first line of a method is called the method
header.
• Calls are in main or another method
• method headers look like:
public static <return type> method( <arguments> )
{
}
• From the header to the closing bracket is called the
method definition.
public class TrivialApplication {
public static void main(String args[]) {
System.out.println("In Main");
method1( );
System.out.println("Back in Main from method 1");
System.out.println( method2( ) );
int number = method3( 5 );
System.out.println("What came back was " + number);
}
public static void method1( ) {
System.out.println("In method 1");
}
public static String method2( ) {
System.out.println("In method 2");
return "Back in Main from method 2";
}
public static int method3( int x ) {
System.out.println("In method 3");
x++;
System.out.println("Just added one to what was sent");
return x;
}
}
First Assignment
• You are to write a complete program that
prompts the user to enter two integer values.
The program should determine the GCD(
Greatest Common Divisor of the two
Integers) and print out all divisors common
to the two integers. Write the entire program
in main.
• Turn that in, and create a second program
with an input method, a findGCD method
and an output method that does the same
thing.