0% found this document useful (0 votes)
1 views

01ComputerBasics

Computer Basics

Uploaded by

Sixmund Byabato
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

01ComputerBasics

Computer Basics

Uploaded by

Sixmund Byabato
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 27

CSE 1223: Introduction to

Computer Programming in Java


Chapter 1 – Computer Basics

1
Computer Basics

 Computer system: hardware + software


 Hardware: the physical components
 Software: the instructions that tell the
hardware what to do

2
Common Hardware Components

 Processor (CPU)
 Central Processing Unit Standard Hardware
 Interprets and executes the Organization
instructions
 Memory
 main & auxiliary
 holds data and instructions Memory
(main & auxiliary)
 Input device(s)
 mouse, keyboard, etc.
 Output device(s) Input
Processor
Output
Devices Devices
 video display, printer, etc. (such as mouse and
(CPU)
(such as video
keyboard) display or printer)
 CPU and memory are
physically housed
together

3
Running a Program

Program—a set of instructions for a computer to follow

Program

Data
Computer Output
(input for the program)

4
Programming Languages

 Needed to write software


 High-level languages (e.g., Java)
 relatively easy for people to write and understand
 not directly understood by computers
 Low-level languages (machine language)
 directly understood by computer
 computer-dependent

5
The Compiler

 A program written in a high-level language


(know as the source code) cannot be
executed directly by the computer
 A compiler is a program that translates
source code into machine code that does the
same thing (known as the object code)

6
Java Program Translation and Execution

Input to
Java Program

Java
Java Java Java Virtual
Source
Compiler Byte-Code Machine
Code

Output of
Java Program

7
Java Translation/Execution cont.

 Java byte-code is portable (hardware-


independent)
 The Java Virtual Machine (JVM) executes
Java byte-code on a real machine
 The java compiler is called javac
 The JVM emulator is called java

8
Algorithmic Thinking

 Algorithm - a set of instructions (steps) for


solving a problem.
 must be precise
 must be complete
 can be written in an arbitrary notation (e.g.,
natural language, programming language,
diagram, mix, etc.)
 Algorithmic thinking is fundamental to computer
science and programming

9
Example of an Algorithm

Algorithm that determines the total cost of a list of items:

1. Write the number 0 on the blackboard.


2. Do the following for each item on the list:
a. Add the cost of the item to the number on the
blackboard.
b. Replace the old number on the board by this sum.
3. Announce that the answer is the number written on the
board.

10
First Java Program
public class FirstProgram
{
public static void main(String[] args)
{
System.out.println("Hello out there.");
System.out.println("How’s it going?");
System.out.println("Good-bye.");
}
}

11
Language Syntax

 Syntax of a language is a set of (grammar)


rules that describe the correct way to write
sentences (programs) in the language.
 Programming languages have a very precise
syntax: If you break the rules, you’ll get one
(or more) errors.

12
Structure of a Java Program

// import needed libraries

public class ProgramName


{
public static void main(String[] args)
{
// statements go here to describe
// actions to be taken by the program
}
}

13
A Java Statement

System.out.println(“some message here”);

 Outputs the message in quotes to the screen


(without the quotes)

14
What Does FirstProgram Do?

 Take a look at the program and see if you


can figure out what the program does.
 How do we do this?
 We “trace” the program
 In other words – go line by line through the
program and ask: “What does this line do?”

15
First Java Program - Trace
Program Line What we see
System.out.println("Hello out there.");

Hello out there.


System.out.println("How’s it going?");

Hello out there.


How's it going?
System.out.println("Good-bye.");
Hello out there.
How's it going?
Goodbye.

16
What Does FirstProgram Do?

 So based on our trace, what does it do?


 It outputs the following:

Hello out there.


How’s it going?
Good-bye.

17
Second Java Program
import java.util.Scanner;
public class EggBasket
{
public static void main(String[] args)
{
int numberOfBaskets=2;
int eggsPerBasket=12;
int totalEggs = numberOfBaskets * eggsPerBasket;
System.out.println("Total number of eggs is "+ totalEggs);
}
}

18
What Does EggBasket Do?

 Take a look at the program and see if you


can figure out what it does.
 Note that unlike our first program, not everything
that EggBasket does is done by displaying things
to the screen
 It does some math as well as displaying things

19
Tracing EggBasket

 To trace EggBasket we need to think about


what is going on in the computer
 We call the internal configuration of the program
the program state – more on this later
 For now, let’s see how we’ll trace the program

20
EggBasket - Trace
Program Line Program state
int numberOfBaskets=2;

numberOfBaskets = 2
int eggsPerBasket=12;

numberOfBaskets = 2
eggsPerBasket = 12
int totalEggs =
numberOfBaskets*eggsPerBasket;
numberOfBaskets = 2
eggsPerBasket = 12
totalEggs = 24
System.out.println("Total eggs: "+
totalEggs);

numberOfBaskets = 2
eggsPerBasket = 12
totalEggs = 24
Outputs to screen:
[Total eggs: 24] 21
Programming Errors

 Syntax errors—violation of language’s syntax


rules, e.g., misspelling a word, forgetting a ;,
etc. Caught by the compiler!
 Runtime errors—execution errors, e.g.,
division by zero.
 Logical errors—the program compiles and
runs without runtime errors, but it does not do
what it is supposed to.

22
Programming Errors
import java.util.Scanner;
public class EggBasket
{
public static void main(String[] args)
{
int numberOfBaskets=2;
int eggsPerBasket=12;
nit totalEggs = numberOfBaskets * eggsPerBasket;
System.out.println("Total number of eggs is "+ totalEggs);
}
}

Syntax Error! Should be int

23
Programming Errors
import java.util.Scanner;
public class EggBasket
{
public static void main(String[] args)
{
int numberOfBaskets=2;
int eggsPerBasket=0;
int totalEggs = numberOfBaskets / eggsPerBasket;
System.out.println("Total number of eggs is "+ totalEggs);
}
}

Runtime Error! Attempt to divide by zero

24
Programming Errors
import java.util.Scanner;
public class EggBasket
{
public static void main(String[] args)
{
int numberOfBaskets=2;
int eggsPerBasket=12;
int totalEggs = numberOfBaskets + eggsPerBasket;
System.out.println("Total number of eggs is "+ totalEggs);
}
}

Logical Error! To find total number of eggs in all


baskets, we need to multiply # of baskets by the eggs
per basket, not add the two together!

25
Another Java Program
import java.util.Scanner;
public class EggBasketEnhanced
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.print(
"Enter the number of eggs in each basket: ");
String eggsStr = keyboard.nextLine();
int eggsPerBasket = Integer.parseInt(eggsStr);
System.out.print("Enter the number of baskets: ");
String basketStr = keyboard.nextLine();
int numberOfBaskets = Integer.parseInt(basketStr);
int totalEggs = numberOfBaskets * eggsPerBasket;
System.out.println(eggsPerBasket + " eggs per basket.");
System.out.println(numberOfBaskets + " baskets.");
System.out.println("Total number of eggs is " + totalEggs);
}
}

26
What Does EggBasketEnhanced Do?

 Take a look at the program and see if you


can figure out what it does.
 There are some things in here we haven’t talked
about yet.
 Can you guess what they’re doing based on their
names and how we’re using them?
 Can you trace it?

27

You might also like