220 likes | 386 Views
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
E N D
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 class public class Point { intx,y; public intgetX(); public void setX(intx_val); ……
Intro to Java Programming • Class example 2: • The Person class public class Person { String name; int age; …… public intgetName(); public intchangeName( 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 • Comments: • // comments to the end of line • /* block comments */
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
Intro to Java Programming • Two types of programs: • Console (text) programs • Graphical programs (event-driven)
Intro to Java Programming • Console programs: • Output: • System.out.print(…); • System.out.println(…);
Intro to Java Programming • Console programs: • Input: Import java.util.Scanner; Scanner inp = new Scanner(System.in); Use nextInt() to input an integer Use nextDouble to imput a double Use nextLine to input a line ……
Intro to Java Programming • Data types • int • double • String • boolean
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
Intro to Java Programming • Program 5: • Read total minutes • Convert the minutes into hours and minutes • Display the result