360 likes | 510 Views
Chapter 1. Introduction to Computers and Java. Chapter 1. 1.1 Computer Basics 1.2 A Sip of Java 1.3 Programming Basics 1.4 Graphics Supplement. 1.1 Computer Basics. The CPU is the brain. Memory is the storage. RAM is volatile. Secondary memory is permanent.
E N D
Chapter 1 Introduction to Computers and Java
Chapter 1 1.1 Computer Basics 1.2 A Sip of Java 1.3 Programming Basics 1.4 Graphics Supplement
Memory is byte addressable 4 byte memory location at address 8 bits = 1 byte
Programs must betranslated Java code Machine code
Using a Compiler<Compile once – run many> Assembly codeLow level Source codeHigh level Compiler Binary codeMachine level
Using an Interpreter<Compile-run cycle> Assembly codeLow level Binary codeMachine level Programexecution Source codeHigh level Interpreter
The Java way usesboth techniques Java Virtual Machine<Interpreter>
Text Application Deconstructed<FirstProgram.java> Program uses the scanner class import java.util.Scanner; public class FirstProgram { public static void main(String[] args) { Inform the user System.out.println("Please enter two numbers and"); System.out.println("I will compute their sum."); Declare variables int n1; int n2; Read from the keyboard Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); Give user an answer System.out.println("Their sum is"); System.out.println(n1 + n2); }// end main() }// end FirstProgram
Text Application Deconstructed<output> Please enter two numbers and I will compute their sum. 10 30 Their sum is 40
GUI Program Deconstructed<HappyFace.java> Classes we need for GUI programs package happyface; import javax.swing.JFrame; import java.awt.Graphics; public class HappyFaceApplet extends JFrame{ @Override public void paint(Graphics canvas) { } } Using drawing commands canvas.drawOval(100, 50, 200, 200); canvas.fillOval(155, 100, 10, 20); canvas.fillOval(230, 100, 10, 20); canvas.drawArc(150, 160, 100, 50, 180, 180);
GUI Program Deconstructed<HappyFace.java> Set window's initial width and height public HappyFace() { setSize(600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); }// end HappyFace() Exit program when window is closed. public static void main(String[] args) { HappyFaceguiWindow = new HappyFace(); guiWindow.setVisible(true); }// end main() }// end HappyFace Initialize the window and make it visible
OOP is based onthree pillars Encapsulation Polymorphism Inheritance
Encapsulation = Information Hiding You can drive a car, but you may not know how an engine works.
Inheritance = Reuse / Extend Objects can be reused Objects can be extended
Polymorphism = React in own way Come back!
Before coding beginsdevelop your algorithms Algorithms come inmany flavors
Once coding beginsbe aware of bugs Syntax – Grammar not followed Run-time – Computer can't honor request Logic – Programmer slip
The Coordinate System (0,0) X (100, 50) Y
Drawing an Oval (0,0) X (100, 50) Y canvas.drawOval(100, 50, 100, 100);
Filling an Oval (0,0) X (100, 50) Y canvas.fillOval(155, 100, 10, 20);
Filling an Oval (0,0) X (100, 50) Y canvas.fillOval(230, 100, 10, 20);
Drawing an Arc (0,0) X (150, 160) Y canvas.drawArc(150, 160, 100, 50, 180, 180);