chapter 1
DESCRIPTION
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. - PowerPoint PPT PresentationTRANSCRIPT
8
Memory is byte addressable
1000
999
998
997
996
995
994
993
0000 0000
0000 0000
0000 0000
0000 0001
1000 0001
1
'A'
4 byte memory locationat address
8 bits = 1 byte
12
Using a Compiler<Compile once – run many>
Source codeHigh level
CompilerAssembly code
Low level Binary codeMachine level
13
Using an Interpreter<Compile-run cycle>
InterpreterSource code
High level
Assembly codeLow level
Binary codeMachine level
Programexecution
17
Text Application Deconstructed<FirstProgram.java>
import java.util.Scanner;
System.out.println("Please enter two numbers and"); System.out.println("I will compute their sum.");
int n1; int n2; Scanner keyboard = new Scanner(System.in); n1 = keyboard.nextInt(); n2 = keyboard.nextInt(); System.out.println("Their sum is"); System.out.println(n1 + n2); }// end main()
public static void main(String[] args) {
public class FirstProgram{
}// end FirstProgram
Program uses the scanner
class
Inform the user
Declare variables Read from
the keyboard
Give user an answer
18
Text Application Deconstructed<output>
Please enter two numbers andI will compute their sum.10 30Their sum is40
19
GUI Program Deconstructed<HappyFace.java>
package happyface;
import javax.swing.JFrame;import java.awt.Graphics;
public class HappyFaceApplet extends JFrame { @Override public void paint(Graphics canvas) {
}}
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);
Classes we need for GUI
programs
Using drawing commands
20
GUI Program Deconstructed<HappyFace.java>
public HappyFace() { setSize(600, 400); setDefaultCloseOperation(EXIT_ON_CLOSE); }// end HappyFace()
public static void main(String[] args) { HappyFace guiWindow = new HappyFace(); guiWindow.setVisible(true); }// end main() }// end HappyFace
Set window's initial width and
height
Initialize the window and
make it visible
Exit program when window is
closed.
26
Encapsulation = Information Hiding
You can drive a car, but you may not know how an engine works.
30
Once coding beginsbe aware of bugs
Syntax – Grammar not followed
Run-time – Computer can't honor request
Logic – Programmer slip