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

CSC204 - Slide 1

Computer science For University Year 2 students Easy to comprehend.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

CSC204 - Slide 1

Computer science For University Year 2 students Easy to comprehend.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

PRINCIPLE OF

PROGRAMMING II
CSC204
Java - General

n Java is:
– platform independent programming
language
– similar to C++ in syntax
– Object Oriented Programming Language
How it works…!
n Java is independent only for one reason:
– Only depends on the Java Virtual Machine
(JVM),
– code is compiled to bytecode, which is
interpreted by the resident JVM,
– JIT (just in time) compilers attempt to
increase speed.
Object-Oriented

n Java supports OOP


– Polymorphism : Many Forms
– Inheritance : SubClass and SuperClass
– Encapsulation : Data hiding
n Java programs contain nothing but
definitions and instantiations of classes
– Everything is encapsulated in a class!
Java Advantages

n Portable - Write Once, Run Anywhere


n Security has been well thought through
n Robust memory management
n Designed for network programming
n Multi-threaded (multiple simultaneous tasks)
n Dynamic & extensible (loads of libraries)
– Classes stored in separate files
– Loaded only when needed
Basic Java Syntax
public class Main
{
public static void main(String[] args)
{
System.out.println("Hello World");
}
}
Primitive Types and Variables
n boolean, char, byte, short, int, long, float, double
n Declaring primitive variables:
float initVal;
int retVal, index = 2;
double gamma = 1.2;
boolean valueOk = false;
Initialization

n If no value is assigned prior to use, then the


compiler will give an error
n Java sets primitive variables to zero or false
in the case of a boolean variable
n All object references are initially set to null
n An array of anything is an object
– Set to null on declaration
– Elements to zero false or null on creation
Declarations
int index = 1.2; // compiler error
boolean retOk = 1; // compiler error
double fiveFourths = 5 / 4; // no error!
float ratio = 5.8f; // correct
double fiveFourths = 5.0 / 4.0; // correct

n 1.2f is a float value accurate to 7 decimal places.


n 1.2 is a double value accurate to 15 decimal places.
Assignment
n All Java assignments are right associative
int a = 1, b = 2, c = 5
a=b=c
System.out.print(
“a= “ + a + “b= “ + b + “c= “ + c)

n What is the value of a, b & c


n Done right to left: a = (b = c);
Basic Mathematical Operators
n * / % + - are the mathematical operators
n * / % have a higher precedence than + or -
double myVal = a + b % d – c * d / b;
n Is the same as:
double myVal = (a + (b % d)) –
((c * d) / b);
Statements & Blocks
n A simple statement is a command terminated by
a semi-colon:
name = “Fred”;
n A block is a compound statement enclosed in
curly brackets:
{
name1 = “Fred”; name2 = “Bill”;
}
n Blocks may contain other blocks
Designing Algorithms for your program
n Writing a program involves designing algorithms and translating algorithms into
code. An algorithm describes how a problem is solved in terms of the actions to
be executed and the order of their execution.
n Algorithms can help the programmer plan a program before writing it in a
programming language. Algorithms can be described in natural languages or in
pseudocode (i.e., natural language mixed with programming code).
n Algorithm Example: Algorithm for Area of a Circle
n 1. Read in the radius.
2. Compute the area using the following formula: area = radius * radius * p
n 3. Display the area.
Writing your program
n When you code, you translate an algorithm into a program.
n Every Java program begins with a class declaration in which the keyword
class, followed by the class name.
n Assume that you have chosen ComputeArea as the class name.
n Also, every Java program must have a main method where program
execution begins. The outline of the program is as follows
1. public class ComputeArea {
public static void main(String[] args) {
2. // Step 1: Read in radius
3. // Step 2: Compute area
4. // Step 3: Display the area
5. }
6. }
Writing your program
n Rather than using x and y as variable names, choose descriptive names:
in this case, radius for radius, and area for area.
n To let the compiler know what radius and area are, specify their data
types.
n Java provides simple data types for representing integers, floating- point
numbers (i.e., numbers with a decimal point), characters, and Boolean
types. These types are known as primitive data types or fundamental
types.
Prog 1: Compute Area
1 public class ComputeArea {
2 public static void main(String[] args) {
3 double radius; // Declare radius
4 double area; // Declare area
5 // Assign a radius
6 radius = 20; // New value is radius
7 // Compute area
8 area = radius * radius * 3.14159;
9 // Display results
10 System.out.println("The area for the circle of radius " + radius + " is " +
area);
11 }
12 }
Reading Input from the Console
n Java uses System.out to refer to the standard output device and
System.in to the standard input device.
n By default the output device is the display monitor, and the input device
is the keyboard.
n Scanner class can be used to create an object to read input from
System.in, as follows:
– Scanner input = new Scanner(System.in);
n The syntax Scanner input declares that input is a variable whose type
is Scanner.
n The whole line Scanner input = new Scanner(System.in) creates a
Scanner object and assigns its reference to the variable input.
Prog 2: Compute Average
import java.util.Scanner; // Scanner is in the java.util package 2
public class ComputeAverage {
public static void main(String[] args) {
// Create a Scanner object
Scanner input = new Scanner(System.in);
// Prompt the user to enter three numbers
System.out.print("Enter three numbers: ");
double number1 = ; input.nextDouble();
double number2 = input.nextDouble();
double number3 = input.nextDouble();
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display result
System.out.println("The average of " + number1 + " " + number2 + " " +
number3 + " is " + average);
Identifiers
n An identifier is a sequence of characters that
consists of letters, digits, underscores (_), and dollar
signs ($).
n An identifier must start with a letter, an underscore
(_), or a dollar sign ($). It cannot start with a digit.
n An identifier cannot be a reserved word
n An identifier can be of any length.
Variables
n Variables are used to store values to be used later in a
program. They are called variables because their values can
be changed.
n Variables are for representing data of a certain type. To use
a variable, you declare it by telling the compiler its name as
well as what type of data it can store.
n The variable declaration tells the compiler to allocate
appropriate memory space for the variable based on its data
type. The syntax for declaring a variable is
n datatype variableName;
n Here are some examples of variable declarations:
– int count; // Declare count to be an integer variable;
– double radius; // Declare radius to be a double variable;
– double interestRate; // Declare interestRate to be a double variable;
Variables contd…
n If variables are of the same type, they can be declared
together, as follows:
– datatype variable1, variable2, ..., variablen;
n The variables are separated by commas. For example,
– int i, j, k; // Declare i, j, and k as int variables
n Variables often have initial values. You can declare a
variable and initialize it in one step. Consider, for instance,
the following code:
– int count = 1;

n You can also use a shorthand form to declare and initialize


variables of the same type together. For example,
– int i = 1, j = 2;

You might also like