63
Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Embed Size (px)

Citation preview

Page 1: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Data Types and Statements

MIT 12043: Fundamentals of ProgrammingLesson 02

S. Sabraz Nawaz

Page 2: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Tracing a program Statements Variables Constants Data Types Arithmetic Calculations Pre and Post increment operators Taking Input from User

Topics Covered

Page 3: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Introducing Programming with an Example

Computing the Area of a Circle

This program computes the area of the circle.

Page 4: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle

of radius " + radius + " is " + area); }}

no valueradius

allocate memory for radius

Page 5: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle

of radius " + radius + " is " + area); }}

no valueradius

memory

no valuearea

allocate memory for area

Page 6: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle

of radius " + radius + " is " + area); }}

20radius

no valuearea

assign 20 to radius

Page 7: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle

of radius " + radius + " is " + area); }}

20radius

memory

1256.636area

compute area and assign it to variable area

Page 8: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle

of radius " + radius + " is " + area); }}

20radius

memory

1256.636area

print a message to the console

Page 9: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

A Statement is the simplest task you can accomplish in Java.

Statements

int othrs=5; System.out.println("netsalary= "+netsal);

You need to put a semi colon ; at theend of a statement.

You need to put a semi colon ; at theend of a statement.

Page 10: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Variables are locations in memory where values can be stored

Variables

Page 11: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Variable is a location in memory Each location in memory has a memory

address, which is a number This long number is inconvenient to use when

we want to access the memory location We give a human understandable name

to refer to this number e.g. age, quantity

The compiler and the interpreter maps this name to the memory address number

Variable Name

Page 12: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

At a given time one value can be stored under the variable

Value of a Variable

quantity

Page 13: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

You need to specify what type of data is to be stored. e.g. int, char

This is because we must instruct how much memory should be reserved by the program to store the value of a variable

The amount of memory needed depends on the maximum of the value we need to store in the variable.

Variable Type

Page 14: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Variable Type…

Page 15: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Java supports eight primitive data types. Eg: int, char…

In Java we write classes and class can be a data type Eg: If you write a class called Student you can use it as

the Student data type

Java Data Types

Page 16: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

These are built into the language itself. Consists of Numeric Types, char type and Boolean

type. Remember String is not a primitive data type in

Java String is a class in Java, thus it is handled as a data

type derived from a class.

Primitive Data Types

Page 17: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Data Types

Name Range Storage Size

byte –27 (-128) to 27–1 (127) 8-bit signed

short –215 (-32768) to 215–1 (32767) 16-bit signed

int –231 (-2147483648) to 231–1 (2147483647) 32-bit signed

long –263 to 263–1 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807)

float Negative range: 32-bit IEEE 754 -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38

double Negative range: 64-bit IEEE 754 -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308

Page 18: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Declaring Variables

int x; // Declare x to be an // integer variable;

double radius; // Declare radius to // be a double variable;

char a; // Declare a to be a // character variable;

Page 19: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Assignment Statements

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;

Page 20: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Declaring Variables

public static void main (String args[]) {int count;String title;boolean isAsleep;...

}

Variables are usually defined at thebeginning. However this need not always

be the case.

Variables are usually defined at thebeginning. However this need not always

be the case.

Page 21: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Declaring Variables

int x, y, z;String firstName, lastName;

Multiple variables can be defined under one type

Multiple variables can be defined under one type

Page 22: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Once declared the variable need to be initialized Initialization – Specify the value we want to store in

the variable

Declaring Variables

int myAge; myAge = 32;String myName = “SaNa";boolean isTired = true;int a = 4, b = 5, c = 6;

You can also initialize variables asthe declaration is done.

You can also initialize variables asthe declaration is done.

Page 23: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Declaring and Initializingin One Step

int x = 1; double d = 1.4;

int age=19;

The above statements are identicalThe above statements are identical

int age;…age = 19;

Page 24: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Variable Names

int age;float $money;char my_char;long _no;String Name7;

A Variable Name should start with an Alphabetical letter or $, or _ symbol

The other characters can include numbersBut you cannot use symbols like @, #, etc

A Variable Name should start with an Alphabetical letter or $, or _ symbol

The other characters can include numbersBut you cannot use symbols like @, #, etc

Page 25: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Variable Names

int my age;float @money;char 6my_char;long no*;

The above names are incorrect.You cannot have spaces and other

special symbols.

The above names are incorrect.You cannot have spaces and other

special symbols.

Page 26: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Variable Names

int qty;String firstName;float basicSal, netSal;

It’s best if you can give suitable (short but meaningful) variable names.

It’s best if you can give suitable (short but meaningful) variable names.

Page 27: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Constants

A named constant is an identifier that represents a permanent value

final datatype CONSTANTNAME = VALUE;

final double PI = 3.14159; final int SIZE = 3;

Page 28: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Numeric Operators

Name Meaning Example Result

+ Addition 34 + 1 35 - Subtraction 34.0 – 0.1 33.9 * Multiplication 300 * 30 9000 / Division 1.0 / 2.0 0.5 % Remainder 20 % 3 2

Page 29: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Integer Division

+, -, *, /, and %

5 / 2 yields an integer 2

5.0 / 2 yields a double value 2.5

5 % 2 yields 1 (the remainder of the division)

Page 30: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Remainder Operator

Remainder is very useful in programming. For example, an even number % 2 is always 0 and an odd number % 2 is always 1. So you can use this property to determine whether a number is even or odd.

Page 31: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Number Literals

A number literal is a constant value that appears directly in the program. For example, 34, 1,000,000, and 5.0 are literals in the following statements:

 

int i = 34;

long x = 1000000;

double d = 5.0;

Page 32: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Integer Literals

An integer literal can be assigned to an integer variable as long as it can fit into the variable.

A compilation error would occur if the literal were too large for the variable to hold. For example, the statement byte b = 1000 would cause a compilation error, because 1000 cannot be stored in a variable of the byte type.

An integer literal is assumed to be of the int type, whose value is between -231 (-2147483648) to 231–1 (2147483647). To denote an integer literal of the long type, append it with the letter L or l. L is preferred.

Page 33: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Floating-Point Literals

Floating-point literals are written with a decimal point.

By default, a floating-point literal is treated as a double type value. For example, 5.0 is considered a double value, not a float value.

You can make a number a float by appending the letter f or F, and make a number a double by appending the letter d or D.

For example, you can use 100.2f or 100.2F for a float number, and 100.2d or 100.2D for a double number.

Page 34: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Scientific Notation

Floating-point literals can also be specified in scientific notation, for example, 1.23456e+2, same as 1.23456e2, is equivalent to 123.456, and 1.23456e-2 is equivalent to 0.0123456. E (or e) represents an exponent and it can be either in lowercase or uppercase.

Page 35: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Arithmetic Expressions

)94

(9))(5(10

5

43

y

x

xx

cbayx

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

Page 36: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

How to Evaluate an Expression

• Though Java has its own way to evaluate an expression behind the scene, the result of a Java expression and its corresponding arithmetic expression are the same. Therefore, you can safely apply the arithmetic rule for evaluating a Java expression.

3 + 4 * 4 + 5 * (4 + 3) - 1 3 + 4 * 4 + 5 * 7 – 1 3 + 16 + 5 * 7 – 1 3 + 16 + 35 – 1 19 + 35 – 1 54 - 1 53

(1) inside parentheses first

(2) multiplication

(3) multiplication

(4) addition

(6) subtraction

(5) addition

Page 37: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Shortcut Assignment Operators

Operator Example Equivalent

+= i += 8 i = i + 8

-= f -= 8.0 f = f - 8.0

*= i *= 8 i = i * 8

/= i /= 8 i = i / 8

%= i %= 8 i = i % 8

Page 38: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Increment and Decrement Operators

Operator Name Description++age preincrement The expression (++age) increments age by

1 and evaluates to the new value in age after the increment.

age ++ postincrement The expression (age ++) evaluates to the original value in age and increments age by 1.

--age predecrement The expression (--age) decrements age by 1 and evaluates to the new value in age after

the decrement. age-- postdecrement The expression (age --) evaluates to

the original value in age and decrements age by 1.

Page 39: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Numeric Type Conversion

Consider the following statements:

byte i = 100;long k = i * 3 + 4;double d = i * 3.1 + k / 2;

Page 40: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Conversion Rules

When performing a binary operation involving two operands of different types, Java automatically converts the operand based on the following rules:

 1.    If one of the operands is double, the other is

converted into double.2.    Otherwise, if one of the operands is float, the

other is converted into float.3.    Otherwise, if one of the operands is long, the

other is converted into long.4.    Otherwise, both operands are converted into int.

Page 41: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Type Casting

Implicit casting double d = 3; (type widening)

Explicit casting int i = (int)3.0; (type narrowing) int i = (int)3.9; (Fraction part is truncated)

byte, short, int, long, float, double

range increases

Page 42: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Escape Sequences for Special Characters

Description Escape Sequence

Backspace \b

Tab \t

Linefeed \n

Carriage return \r

Backslash \\

Single Quote \'

Double Quote \"

Page 43: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

The String Type

The char type only represents one character. To represent a string of characters, use the data type called String. For example,  String message = "Welcome to Java"; String is actually a predefined class in the Java library just like the System class. The String type is not a primitive type. It is known as a reference type. Any Java class can be used as a reference type for a variable.

Page 44: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

String Concatenation

// Three strings are concatenatedString message = "Welcome " + "to " + "Java"; // String Chapter is concatenated with number 2String s = "Chapter" + 2; // s becomes Chapter2 // String Supplement is concatenated with character BString s1 = "Supplement" + 'B'; // s1 becomes SupplementB

Page 45: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

A program when given three marks of an exam which calculates and prints the total and the average.

Exercises 1

Page 46: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

A Program when given the Currency Rate of a US Dollar. Calculates and prints the a Sri Lankan Ruppee amount into US Dollars.

Exercises 2

Page 47: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Write a program to input how many notes, coins of denominations of 1000/=, 500/=, 200/=, 100/= 50/=,20/=,10/=,5/=, 2/= and 1/= are available.

Print the total amount

Exercises 3

Page 48: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Exercises 4

Write a program that converts a Fahrenheit degree to Celsius using the formula:

)32)(( 95 fahrenheitcelsius

Page 49: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Taking User Inputs

Page 50: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

The Scanner class is a class in java.util, which allows the user to read values of various types.

The Scanner looks for tokens in the input. A token is a series of characters that ends with what Java calls whitespace. A whitespace character can be a blank, a tab character, a carriage return, or the end of the file. 

Using Scanner Class

Page 51: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Using Scanner Class

Method Returns

int nextInt() Returns the next token as an int.

long nextLong() Returns the next token as a long.

float nextFloat() Returns the next token as a float.

double nextDouble()

Returns the next token as a double.

String next()Finds and returns the next complete token from this scanner and returns it as a string; a token is usually ended by whitespace such as a blank or line break.

String nextLine() Returns the rest of the current line, excluding any line separator at the end.

Page 52: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Using Scanner Class

Fundamentals of Programming by SaNa@seu

01

02

03

Page 53: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Using Scanner Class…

Page 54: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Using Scanner Class…

Page 55: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Using Scanner Class…

Fundamentals of Programming by SaNa@seu

Page 56: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Solution for Restaurant Bill – Exam - Answer

Page 57: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Solution for Restaurant Bill – Classroom Exam

Page 58: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Exercise 01

Write and run a Java program that prompts the user for his or her last name and first name separately and then prints a greeting like this:

Enter your name: SaNaEnter your first name: SamsHello, SaNa Sams

Page 59: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Exercise 02

Write and run a Java program that inputs an integer that represents a temperature on the Fahrenheit scale and then computes and prints its equivalent Celsius value.

Use the conversion formula C=5(F-32)/9

Page 60: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Exercise 03

Write and run a Java program that inputs an integer that represents a temperature on the Celsius scale and then computes and prints its equivalent Fahrenheit value.

Use the conversion formula F= 1.8C + 32

Page 61: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Exercise 04

A university pays its Academic Staff Academic Allowance 39% of Basic Salary, Research Allowance 25% of Basic Salary, and Cost of Living Allowance 5,850/=. And deducts UPF 8% of the Basic Salary. Write a Java program to input the Basic Salary. Calculate the above and display them all with Net Salary

Page 62: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

Assignment

1. What is meant by Casting in Java? Explain with suitable examples

2. What is Constant? Explain with suitable examples

Submit on or before 10th January 2014

Page 63: Data Types and Statements MIT 12043: Fundamentals of Programming Lesson 02 S. Sabraz Nawaz Fundamentals of Programming by SaNa@seu

Fundamentals of Programming by SaNa@seu

End of Lecture