Upload
lillian-dickerson
View
220
Download
1
Embed Size (px)
Citation preview
Intro to Java Programming A computer follows the
instruction precisely and exactly. Anything has to be declared and
defined before it can be used. The only way to learn
programming is to write many programs.
Intro to Java Programming Java is an OOP language
An object has fields (attributes, properties) and methods (functions)
A class defines an object
Intro to Java Programming Class example 1:
The point classpublic class Point {
int x,y;public int getX();public void setX(int x_val);……
Intro to Java Programming Class example 2:
The Person classpublic class Person {
String name;int age;……public int getName();public int changeName( String new_name);protected void getAge();……
Intro to Java Programming For OOP programming, the
most important aspect is how to define and implement classes.
Almost all executable statements are in classes.
Intro to Java Programming Eclipse: Integral Development
Environment (IDE) The name of the main class
and the name of the saved file must be the same.
The main class is the class that contains the main method.
Intro to Java Programming Every stand-alone program
has to have a main method.Public static void main(…)
The entry point of the program
Intro to Java Programming Errors:
Syntax errors: the compiler picks them up.
Run-time errors. Logic errors.
Intro to Java Programming Special characters:
{ … } Block () Methods or expressions [ … ] Array “ … “ String ; End of statement
Intro to Java Programming Two types of programs:
Console (text) programs Graphical programs (event-driven)
Intro to Java Programming Console programs:
Input:Import java.util.Scanner;Scanner inp = new
Scanner(System.in);Use nextInt() to input an integerUse nextDouble to imput a doubleUse nextLine to input a line……
Intro to Java Programming Operators
Math: + - * / %pay attention to integer division
Relational: == != > >= < <=
Logical: && || ^
Intro to Java Programming Program 1:
Read 3 floating numbers Calculate their average Display the results
Intro to Java Programming Program 2:
Read the radius of a circle Calculate the area of the circle
Display the result
Intro to Java Programming Program 2:
Read the radius of a circle Calculate the area of the circle
Display the result Named constant pi
Intro to Java Programming Program 3:
Read an integer between 10 and 99
Extract the left and right digits of the number
Display the result Hint: use / and %
Intro to Java Programming Program 4:
Read two integers between 10 and 99
Do multiplication Display the result