CSC 141 Introduction to Computers

CSC 141 Introduction to Computers

1-1

Java Programming

Chapter 1 Overview of Programming and Problem Solving

1.1 Overview of Programming

Q: What is a computer? Which one of the following can be considered a computer?

- calculator

- cash register

- personal computing device or palm device

Q: What functional components does a computer have?

A:

- CPU : central processing unit : control and arithmetic

- Memory (main memory) : storing data and instructions

- Input unit : entering data or instructions from an input device into memory

- Output unit : displaying/printing out data from memory

Q: What can a computer do?

A:

- arithmetic operations

- logic operations

- storage of data

Q: How can we control a computer to perform these functions?

A:

- via programs

Q: What is a program?

A:

A program is defined as a sequence of instructions that a computer executes.

Q: Who wrote programs?

A:

We call these people programmers who wrote programs as their profession.

Q: What do we call the process of writing programs?

A:

coding or programming.

1.2 How do we write a program?

A computer cannot analyze a problem and come up a solution by itself. When IBM computer once executes the program called DeepBlue beat the world chess champion, the computer was performing actions instructed by a team of highly trained computer programmers. Hence, problem solving using computer must go through a process: problem analysis and coding.

Problem Analysis –

- requirement analysis: identifying the functions of a problem and the problem entities involved

- design specification: designing the program operations and the program entities

- develop general solutions (algorithm): developing the execution sequence of the operations

Coding –

- writing the program based on the algorithm

- compiling the program into machine language which can be understood by a computer using javac command.

- testing the program (sometimes called running or executing the program) using the command java.

- Maintenance: the process to modify your code and recompile/execute/test your modification

1.3 Details of Problem Analysis Phase

Q: What is an algorithm?

A:

- A set of well-defined instructions (commands, orders, etc.) for solving a problem

- It is the base of a program.

- Sometimes, we consider an algorithm the logic of a program.

Q: Any example of algorithm?

A: To go from one city to another, say from North Wilmington near the border of PA and DE to West Chester, you need to follow the driving direction as follows:

  1. Getting on I-95 going south
  2. As you approach I-202, if it is not congested, get on I-202, continue driving for 35 minutes
  3. As you approach I-202, if it is congested, continue going south,

a get off at I-52 exit

b continue driving for 40 minutes

Q: It is an algorithm. But, is this a good algorithm? NO.

(1) in step 2, it doesn’t specify “going north”. (*)

(2) in step 2, what do we mean “if it is congested”? (**)

(3) in step 3-a, it doesn’t specify “going north”. (*)

(4) in step 2 and 3, continue driving for 35 minutes or 40 minutes may not reach West Chester. (*)

(*) Incorrect specification and logic error

(**) not well-defined

1.4 Details of Coding Phase

Q: How do we write a program?

A: We need to “convert” the detailed steps in an algorithm into statements defined in Java or C++, which is a programming language. We generally called the program written in Java/C++ a source program. Programming languages, such as Java, C++, C, FORTRAN, are called high-level languages. Computer usually doesn’t recognize high-level languages. Hence, a special program called compiler is needed to do the translation from high-level language to machine language, which can be recognized by a computer. Normally, a source program is translated into a machine code. But, Java programs are translated into intermediate programs written in a Java Byte-code. The intermediate code will be executed on a Java Virtual Machine that run on top of a machine.

Let’s summarize the description:

programming language: a set of rules, symbols, and special words used to construct a computer program.

Source Program: the program written in high-level language.

Object Program: the program written in machine language that a computer can understand.

Compiler: a program which accepts a source program and translates it into an object program.

Source Program Compiler Object Program Linker Executable

(C, C++ etc.)

For Java Programs:

Intermediate

Source Program Compiler Object Program Java Virtual

(Java) (Byte-Code) Machine

1.5 Computer Software and Hardware

Software: includes all programs, which could be developed by computer venders, application users.

Hardware: all computer components including input units, memory, CPU, and output units. For example, a keyboard, monitor, RAM, printer, even connectors and cables are all considered computer hardware.

Peripherial devices: the general term for all input/output devices, including auxiliary storage devices, i.e., disk drives.

1.6 Introduction to Java Programming Language (or Simply Java)

Java is considered an object-oriented programming language. An object is a representation of a problem entity. For example, if we are writing a program to compute student grades, we need to represent a student and his/her grades. A student or his/her grades are called Problem Entities. We can use an integer or a floating-point number (i.e., a number with a decimal point in it) to represent his/her grades. We can call the integers or floating-point numbers as Program Entities. Each instance of a problem entity is represented by an instance of a program entity. For example, if a student’s exam#1 grade is defined as a floating-point number with the name exam1, we can say that exam1 is an instance of a floating-point. In this example, a floating-point number is called an object. So an object is actually a representation of a problem entity. In Java, we use a class program entity (or a Java programming feature) to implement an object.

An object actually consists of the data (e.g., an integer number or a floating-point number) and the operations that the data are involved. For example, a student can be represented as an object. However, we cannot represent a student object with a single integer or a single floating-point number. Why? To represent a student object, we must define several data items such as the name of a student, his/her id, the exam grades for all exams, the GPA, etc. In this course series, how to represent an object effectively and manipulate these objects efficiently are two important objectives.

Generally speaking, an object can be characterized by two things: data items and methods. A data item is like the name or id of a student in a student object. What about methods? Methods are ways to manipulate these data items. For example, when computing the final average grade of a student, we need to use an operation Compute_Average to accomplish it. The name Compute_Average is called an operation.

When we write a program, we need to define the classes we need before we begin implementing the major logic to solve a problem. For example, if we have defined a student class and the method Compute_Average, we can call a student a, and computing the average can be achieved by writing “a.Compute_Average();”. In this course, we will begin to learn how to represent various objects including data items and methods.

Java programs can be written and executed in two approaches: applications or Applets. An application is a stand-alone program consisting of a “public static void main( String[] args)” method and can be executed by using the “java” command; an application is a Java program without the main() method and must be executed with another “.html” file. In the following sections, we will introduce both approaches.

1.7 A Java Application Program

In this section, we want you to type in the following into a folder with the name JavaPrograms. Use the name Student.java for the first part in the following and FirstProgram.java for the second part. After you finish typing, you can bring up a Command Prompt Window and enter the following commands:

>javac FirstProgram.java

>java FirstProgram

After you enter the javac command, you may see some error messages being displayed on the screen. Learning how to understand these error messages is considered part of the learning process. It is due to the limitations of a compiler, some error messages are not clear enough (from a programmer’s point of view). But that is what you may see and sometimes, the only clue you have. For this program, you need to make sure if you enter everything correctly. You need to check the spelling, cases, and even indentation.

Listing 1.1 A Java Application Program

import java.util.Scanner;

public class FirstProgram

{

public static void main(String[] args)

{

String name;

int grade;

Scanner scan = new Scanner (System.in);

System.out.println("Please enter your name");

name = scan.nextLine( );

System.out.println("Please enter your grade ");

grade = scan.nextInt( );

System.out.println("Hello " + name + "! Your grade is " + grade);

}

}

Our focus for CSC141 will be on how to use various Java programming features to write programs for solving various problems.

1.8 A Java Applet

There is another form of a Java program. (As matter of fact, there are many other formats, such as Servlets, MIDlets, etc.) We will play with the Applet here. However, the complete discussion will be deferred to CSC142 or later. You will again type this program and store it with the file name C:\Einstein.java (Listing 1.2).

In order to execute this Applet, you need to use a Command Prompt window or Web browser. Before you can execute this Applet, you need another .html file Listing 1.3.

Listing 1.2 (copied from Lewis's book)

------

import javax.swing.JApplet;

import java.awt.*;

public class Einstein extends JApplet

{

public void paint (Graphics page)

{

page.drawRect(50, 50, 40, 40);

page.drawRect (60, 80, 225, 30);

page.drawOval(75, 65, 20, 20);

page.drawLine(35, 60, 100, 120);

page.drawString("Out of clutter, find simplicity.", 110, 70);

page.drawString("-- Albert Einstein", 130, 100);

}

}

Listing 1.3 (Copied from lewis's book) (saved as a text-only file with the .html extension)

------

<html>

<head>

<title>Testing Applets</title>

</head>

<body>

<applet code="Einstein.class" width="350" height="175"> </applet>

</body>

</html>

You simply type these two lines and store the result into a file C:\testapplet.html. You need to enter the following commands:

>javac C:\Einstein.java

>appletviewer testapplet.html

or bring up a Web browser and enter the URL "C:\testapplet.html". (Assuming that the files Einstein.class and testapplet.html are all stored under C:\)

EXERCISES

1. Find another Java program in your textbook and enter the program, compile it with javac, and execute it with the java command.

2. Find one statement and change the spelling of one of the lines in your working progrma and recompile you code. What do you see?

3. Write a Java program to allow a user to enter an integer and display the following (if a user enters 12345):

** ** ** 12345 ** ** **