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

Object Oriented Programming

This document provides an introduction to computer programming concepts and Java programming. It discusses what programming is, the components of a basic "Hello World" Java program, and how to write Java programs to calculate the average of three numbers and calculate an employee's gross pay. The key steps in writing Java programs are identifying the problem, breaking it down into discrete steps, writing code for each step such as getting user input, performing calculations, and outputting results. The document also covers programming language basics like variables, data types, and using the Scanner class to get user input.

Uploaded by

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

Object Oriented Programming

This document provides an introduction to computer programming concepts and Java programming. It discusses what programming is, the components of a basic "Hello World" Java program, and how to write Java programs to calculate the average of three numbers and calculate an employee's gross pay. The key steps in writing Java programs are identifying the problem, breaking it down into discrete steps, writing code for each step such as getting user input, performing calculations, and outputting results. The document also covers programming language basics like variables, data types, and using the Scanner class to get user input.

Uploaded by

Bernard
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 61

Introduction to Computer

Programming

Getting Started: An Introduction to


Programming in Java
What Is Programming?
• Computers cannot do all the wonderful
things that we expect without instructions
telling them what to do.

• Program – a set detailed of instructions


telling a computer what to do

• Programming – designing and writing


computer programs

• Programming language – a language used


to express computer programs.
A First Program
Class header Open braces mark
the beginning Method header

public class MyFirstjava {


public static void main(String[] args) {
System.out.println
("This is my first Java program.");
}
}

statements
Close braces mark
the end
A First Program – What Does It Do?
System.out.println
("This is my first Java program.");

Prints the message


This is my first Java program.

Ends the line


A second program
Problem – write a program which can find the
average of three numbers.

Let’s list the steps that our program must perform to


do this:

• Add up these values


• Divide the sum by the number of values
• Print the result

Each of these steps will be a different statement.


Writing Our Second Program
• Add up these values sum = 2 + 4 + 6;

• Divide the sum by the number of values

• Print the result

sum = 2 + 4 + 6;
an assignment statement
Assignment Statements
• Assignment statements take the form:

variable = expression

Memory location where


the value is stored Combination of
constants
and variables
Expressions
• Expressions combine values using one of
several operations.

• The operations being used is indicated by


the operator:

+ Addition
- Subtraction
* Multiplication
/ Division
Expressions – Some Examples
2+5
4 * value
x/y
Writing Our Second Program
• sum = 2 + 4 + 6;

• Divide the sum by the average = sum / 3;


number of values

• Print the result

Names that describe what


the values represent
Writing Our Second Program
• sum = 2 + 4 + 6
• average = sum / 3;

• Print the result

System.out.println(?The average is ?
+ average);
The output method variable
name
Writing Our Second Program
public class Average3 {
public static void main(String[] args) {
int sum, average;
sum = 2 + 4 + 6;
average = sum / 3;

System.out.println
("The average is " +
average);
}
}
Tells the computer that sum and average are integers
Writing Our Second Program
public class Average3a {
public static void main(String[] args) {
int sum;
int average;
sum = 2 + 4 + 6;
average = sum / 3;
System.out.println
("The average is " +average);
}
}
We could also write this as two separate declarations.
Variables and Identifiers
• Variables have names – we call these names
identifiers.

• Identifiers identify various elements of a


program (so far the only such element are
the variables.

• Some identifiers are standard (such as


System)
Identifier Rules
• An identifier must begin with a letter or an
underscore

• Java is case sensitive upper case (capital) or lower


case letters are considered different characters.
Average, average and AVERAGE are three
different identifiers.

• Numbers can also appear after the first character.

• Identifiers can be as long as you want but names


that are too long usually are too cumbersome.

• Identifiers cannot be reserved words (special


words like int, main, etc.)
Another Version of Average
• Let’s rewrite the average program so it
can find the average any 3 numbers we
try:

• We now need to:

1. Find our three values


2. Add the values
3. Divide the sum by 3
4. Print the result
Writing Average3b
This first step becomes:

1.1 Find the first value


1.2 Find the second value
1.3 Find the third value
2. Add the values
3. Divide the sum by 3
4. Print the result
Writing Avg3 (continued)
Since we want the computer to print out some
kind of
prompt, the first step becomes:

1.1.1 Prompt the user for the first value


1.1.2 Read in the first value
1.2.1 Prompt the user for the second value
1.2.2 Read in the second value
1.3.1 Prompt the user for the third value
1.3.2 Read in the third value
2. Add the values
3. Divide the sum by 3
4. Print the result
Writing Avg3 (continued)
We can prompt the user with:

1.1.1 System.out.println
("Enter the first value ?");
1.1.2 Read in the first value
1.2.1 System.out.println
("Enter the second value ?");
1.2.2 Read in the second value
1.3.1 System.out.println
("Enter the third value ?");
1.3.2 Read in the third value

2.Add the values


3. Divide the sum by 3
4. Print the result
The Scanner Class
• Most programs will need some form of
input.

• At the beginning, all of our input will come


from the keyboard.

• To read in a value, we need to use an object


belonging to a class called Scanner:
Scanner keyb = new Scanner(System.in);
Reading from the keyboard
• Once we declare keyb as Scanner, we can
read integer values by writing:

variable= keyb.nextInt();
Writing the input statements in Average3b

We can read in a value by writing:


System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
2. Add the values
3. Divide the sum by 3
4. Print the result
Writing the assignments statements in Average3b
System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
sum = value1 + value2
+ value3;
3. Divide the sum by 3
4. Print the result Adding up the three values
Writing the assignments statements in Average3b

System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
sum = value1 + value2
+ value3;
3. Divide the sum by 3
4. Print the result Adding up the three values
Writing the output statement in Average3b

System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
sum = value1 + value2 + value3;
average = sum / 3;

System.out.println("The average is "


+ average);
import java.util.Scanner;
public class Average3b {
public static void main(String[] args) {
int sum, average;
Scanner keyb = new Scanner(System.in);
System.out.println
("What is the first value\t?");
int value1 = keyb.nextInt();
System.out.println
("What is the second value\t?");
int value2 = keyb.nextInt();
System.out.println
("What is the third value\t?");
int value3 = keyb.nextInt();
sum = value1 + value2 + value3;
average = sum / 3;
System.out.println("The average is "
+ average);
}
}
Another example – calculating a payroll

• We are going to write a program which calculates


the gross pay for someone earning an hourly
wage.
• We need two pieces of information:
– the hourly rate of pay
– the number of hours worked.
• We are expected to produce one output: the gross
pay, which we can find by calculating:
– Gross pay = Rate of pay * Hours Worked
Our Design for payroll

1. Get the inputs


2. Calculate the gross pay
3. Print the gross pay
We can substitute: 1.1 Get the rate
1.2 Get the hours
Developing The Payroll Program
We can substitute:
1.1.1 Prompt the user for the rate
1.1.2 Read the rate
1.2.1 Prompt the user for the hours
1.2.2 Read the hours e hours

1.1 Get the rate


1.2 Get the hours

2. Calculate the gross pay


3. Print the gross pay
Coding the payroll program

• Before we code the payroll program, we


recognize
that the values (rate, hours and gross) may
not necessarily be integers.
• We will declare these to be double, which
means
that they can have (but do not have to have)
fractional parts.
• In Java, we usually declare our variables
where
they first appear in the program.
Developing The Payroll Program (continued)
1.1.1 Prompt the user for the rate
1.1.2 Read the rate

1.2.1 Prompt the user for the hours


1.2.2 Read the hours
2. Calculate the gross pay
3. Print the gross pay

System.out.println("What is your hourly pay rate?");


double rate = keyb.nextDouble();
Developing The Payroll Program
(continued)
System.out.println
("What is your hourly pay rate?");
double rate = keyb.nextDouble();

1.2.1 Prompt the user for the hours


1.2.2 Read the hours
2. Calculate the gross pay
3. Print the gross pay

System.out.println("How many hours did you work?");


double hours = keyb.nextDouble();
Developing The Payroll Program
(continued)
System.out.println
("What is your hourly pay rate?");
double rate = keyb.nextDouble();
System.out.println
("How many hours did you work?");
double hours = keyb.nextDouble();

2. Calculate the gross pay


3. Print the gross pay

double gross = rate * hours;


Developing The Payroll Program (continued)
System.out.println
("What is your hourly pay rate?");
double rate = keyb.nextDouble();
System.out.println
("How many hours did you work?");
double hours = keyb.nextDouble();
double gross = rate * hours;

3. Print the gross pay

System.out.println("Your gross pay is $"


+ gross);
import java.util.Scanner;
public class Payroll {
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
System.out.println
("What is your hourly pay rate?");
double rate = keyb.nextDouble();
System.out.println
("How many hours did you work?");
double hours = keyb.nextDouble();
double gross = rate * hours;
System.out.println
("Your gross pay is $“+ gross);
}
}
Comments
• Our program is a bit longer than our previous programs
and if we did not know how to calculate gross pay, we
might not be able to determine this from the program
alone.
• It is helpful as programs get much longer to be able to
insert text that explains how the program works. These are
called comments. Comments are meant for the human
reader, not for the computer.
• In Java, anything on a line after a double slash (//) is
considered a comment.
• Longer comments can also be contained between /* and
*/
#include <iostream.h>
// This program calculates the gross pay for an hourly
// worker.
// Inputs - hourly pay rate and number of hours worked
// Output - Gross pay
int main(void)
{
float rate, hours, gross;
// Get the hourly pay rate
cout << "What is your hourly pay rate ? ";
cin >> rate;
// Get the number of hours worked
cout << "How many hours did you work ? ";
cin >> hours;
// Calculate and display the gross pay
gross = rate * hours;
cout << "Your gross pay is $" << gross << endl;
return(0);
}
import java.util.Scanner;
public class Payroll {

// This program calculates the gross pay for an


// hourly worker
// Inputs - hourly rate and hours worked
// Output - Gross pay

public static void main(String[] args) {


Scanner keyb = new Scanner(System.in);

// Get the hourly rate


System.out.println
("What is your hourly pay rate?");
double rate = keyb.nextDouble();
// Get the hours worked

System.out.println
("How many hours did you work?");
double hours = keyb.nextDouble();

// Calculate and display the gross pay


double gross = rate * hours;
System.out.println
("Your gross pay is $"+ gross);

}
}
Character Data
• All of our programs so far have used
variables to store numbers, not words.

• We can store single characters by writing:


char x, y;
– x and y can hold one and only one character

• For now, we use character data for input


and output only.
Character Strings
• We are usually interested in manipulating more
than one character at a time.
• We can store more than one character by writing:
String s = new String();
• If we want s can hold to have some initial value,
we can write:
String s
= new String("Initial value");
• For now, we use character data for input and
output only.
A program that uses a character variable

import java.util.Scanner;
public class Polite {
// A very polite program that greets you by name
public static void main(String[] args) {
String name = new String();
Scanner keyb = new Scanner(System.in);
// Ask the user his/her name
System.out.println
("What is your name?");
name = keyb.next();
// Greet the user
System.out.println
("Glad to meet you, " + name);
}
Using Stepwise Refinement to
Design a Program

• You should noticed that when we write a


program, we start by describing the steps
that our program must perform and we
subsequently refine this into a long series of
more detailed steps until we are writing
individual steps. This is called stepwise
refinement.

• Stepwise refinement is one of the most


basic methods for developing a program
Example – A program to convert

pounds to kilograms
• Our program will convert a weight
expressed in pounds into kilograms.
– Our input is the weight in pounds.
– Our output is the weight in kilograms
– We also know that
Kilograms = Pounds / 2.2

Pounds to Kilograms Program (continued)


• Our program must:
1. Get the weight in pounds
2. Calculate the weight in kilograms
3. Print the weight in kilograms
Pounds to Kilograms Program (continued)
• Our program must:

1. Get the weight in pounds


2. Calculate the weight in kilograms
3. Print the weight in kilograms

1.1 Prompt the user for the weight in pounds


1.2 Read the pounds
Pounds to Kilograms Program (continued)
• Our program must:

1.1 Prompt the user for the weight in pounds


1.2 Read the pounds

2. Calculate the weight in kilograms


3. Print the weight in kilograms

System.out.println
("What is the weight in pounds?");

double lbs = keyb.nextInt();


Pounds to Kilograms Program
(continued)
System.out.println
("What is the weight in pounds?");
double lbs = keyb.nextInt();

2. Calculate the weight in kilograms


3. Print the weight in kilograms

double kg = lbs / 2.2;


Pounds to Kilograms Program
(continued)
System.out.println
("What is the weight in pounds?");
double lbs = keyb.nextInt();
double kg = lbs / 2.2;

3. Print the weight in kilograms

System.out.println
("The weight is " + kg+ " kilograms");
Pounds to Kilograms Program
(continued)
System.out.println
("What is the weight in pounds?");
double lbs = keyb.nextInt();
double kg = lbs / 2.2;

3. Print the weight in kilograms


System.out.println
("The weight is " + kg+ " kilograms");
import java.util.Scanner;
public class ConvertPounds {

// Convert pounds to kilograms


// Input - weight in pounds
// Output - weight in kilograms
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);

// Get the weight in pounds


System.out.println
("What is the weight in pounds?");
double lbs = keyb.nextInt();
// Calculate and display the weight in
// kilograms
double kg = lbs / 2.2;
System.out.println(
"The weight is " + kg + " kilograms");
}
}
Another Example – The Area of A Rectangle

• Our program will calculate the area of a


rectangle.

– Our input is the length and width.


– Our output is the area.
– We also know that

Area = Length * Width


Our Program’s Steps
1. Find the length and width

2. Calculate the area

3. Print the area


Our Program’s Steps (continued)

1. Find the length and width

2. Calculate the area


3. Print the area

1.1 Find the length


1.2 Find the width
Our Program’s Steps (continued)
1.1 Find the length
1.2 Find the width

2. Calculate the area


3. Print the area

1.1.1 Prompt the user for the length


1.1.2 Read the length
1.2.1 Prompt the user for the width
1.1.2 Read the width
Our Program’s Steps (continued)
1.1.1 Prompt the user for the length
1.1.2 Read the length
1.2.1 Prompt the user for the width
1.1.2 Read the width

2. Calculate the area


3. Print the area

System.out.println("Enter the length?");


double length = keyb.nextDouble();
System.out.println("Enter the width?");
double width = keyb.nextDouble();
Our Program’s Steps (continued)
System.out.println("Enter the length?");
double length = keyb.nextDouble();
System.out.println("Enter the width?");
double width = keyb.nextDouble();

2. Calculate the area


3. Print the area

double area = length * width;


Our Program’s Steps (continued)
System.out.println("Enter the length?");
double length = keyb.nextDouble();
System.out.println("Enter the width?");
double width = keyb.nextDouble();
double area = length * width;

3. Print the area

System.out.println("The area is " + area);


import java.util.Scanner;
public class CalculateArea {

// Calculates the area of a rectangle


// Inputs - The length and width of the rectangle
// Output - The area of the rectangle
public static void main(String[] args) {
Scanner keyb = new Scanner(System.in);
// Print an explanatory message for the user
System.out.println
("Given the width and length of a
rectangle");
System.out.println
("this program calculates its area." );
// Get the inputs
System.out.println
("Enter the length?");
double length = keyb.nextDouble();
System.out.println
("Enter the width?");
double width = keyb.nextDouble();
// Calculate and display the area
double area = length * width;
System.out.println
("The area is " + area);
}
}

You might also like