Tutorial 1 - University of Windsorcs.uwindsor.ca/60-212/tutorials/Tutorial1.pdf · Tutorial 1...

Post on 24-Mar-2020

8 views 0 download

Transcript of Tutorial 1 - University of Windsorcs.uwindsor.ca/60-212/tutorials/Tutorial1.pdf · Tutorial 1...

Tutorial 1

Agenda

Laboratory 1

Assignment 1

Installing Eclipse

A problem solved using Eclipse

Arithmetic expression and Assignments

Conditions

Lab 1

Your first JAVA Application

• Objectives of this first Lab is to:

– Learn how to write a simple JAVA

application

– Compile and run a JAVA application

In this laboratory you have to write a JAVA

application that finds a prime number.

Remember! A number is a prime number if it can

be divided only by 1 and itself.

Examples:

37 is a prime

35 is not a prime

Laboratory 1

Work to be done a) Your Java program includes the value of a

number called, say numberSupplied.

b) Your program then must compute and print

the first prime number greater than

numberSupplied.

Example 1

If numberSupplied is 20, your program

should print the first prime number greater

than 20. The following message must be

printed:

The first prime number greater than 20 is 23

Work to be done(Cont’d)

Example 2

If numberSupplied is 44, your program

should print the first prime number greater

than 44. The following message must be

printed:

The first prime number greater than 44 is 47

Tasks to be performed

a) Compute the first prime number greater

than numberSupplied

b) Display the results to the user.

Assignment 1 You have to write an application in Java to solve the

following problem.

• In cryptarithmetic puzzles, mathematical equations are

written using letters. Each letter corresponds to a digit

from 0 to 9 but no two letters can be the same. Here is a

sample problem:

SEND + MORE = MONEY

• A solution to the puzzle is S = 9, R = 8, O = 0, M = 1, Y =

2, E = 5, N = 6 and D = 7.

S E N D 9 5 6 7

M O R E 1 0 8 5

M O N E Y 1 0 6 5 2

Assignment 1 (Cont’d)

• Write a program that finds a solution to the

puzzle of

TOO + TOO + TOO + TOO = GOOD

TOO

TOO

TOO

TOO

GOOD

Assignment 1(Cont’d) Idea:

1) Use a nested loop for each letter used in the puzzle (T, O, G and

D).

2) The loops would systematically assign the digits from 0 to 9 to

each letter.

3) Exclude those combinations of values of T, O, G, D where the

same digit is assigned to more than one letter.

4) Check if all conditions are satisfied. If so display the values

assigned to T, O, G, D.

Installing Eclipse

• Download JDK from the oracle website.

• Set the path properly

• Download eclipse from eclipse.org

• Unzip it and put the eclipse directory under

the root directory.

• create a short cut for eclipse on your

desktop.

Steps to remember Step 1) start eclipse

Step 2) Start a workspace. I selected 212_Fall14 a directory I had

created to hold all my work for this term.

Step 3) Create a new java project. I called mine LabAssign1. This will

include both my lab and my assignment for this week.

Step 4) In the package explorer, under this project, you will see src

(standing for source). Select it.

Step 5) Create a new package. This is a directory under which your java

class definitions will be stored. I called my package aPackage.

Step 6) Create a new class file. This will contain your java source code.

At this point we are only creating Java applications. So give a name to

your class (for example Lab1) and select the method stub main.

See next slide for what you get.

Resulting file package aPackage;

public class Lab1 {

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

}

}

Code after typing one line

package aPackage;

public class HelloWorld {

/**

* @param args

*/

public static void main(String[] args) {

System.out.println("Hello How are you?");

}

}

Select Run -> Run As -> 1 Java application

Use of meaningful variable names

Good indentation rules

Adequate documentation is essential.

Look at the Java coding conventions I

have posted on the web.

A problem to solve

• You wish to purchase a car for $25000. You

are making monthly payments of $1000. The

interest rate is 1% per month, calculated

monthly.

• Display a table showing how much you pay

every month and the remaining loan amount.

Ideas:

Initial balance = $25000.00

At the end of the first month,

i) interest due = $250.00

ii) current balance before payment = $25250.00

iii) payment made = 1000.00

iv) current balance after payment = $24250.00

At the end of the second month,

i) interest due = $242.50

ii) current balance before payment = $24492.50

iii) payment made = 1000.00

iv) current balance after payment = $23492.50

And so on until the loan is paid off.

( I have deliberately glossed over some details)

Initialize the value of current balance and interest rate.

While (loan is not paid off)

i) calculate interest on current balance

ii) calculate payment amount.

iii) calculate new balance after payment.

iv) display month payment is made and the new balance.

public class CarLoan {

public static void main(String a[]){

Initialize the value of current balance and interest rate.

While (loan is not paid off)

i) calculate interest on current balance

ii) calculate payment amount.

iii) calculate new balance after payment.

iv) display month payment is made and

the new balance.

}

}

public class CarLoan {

public static void main(String a[]){

double currentBalance, interestRate, interestPayment,

paymentMade;

int month = 0;

currentBalance = 25000.00;

interestRate = 0.01;

while (currentBalance > 0.0) {

month ++;

interestPayment = currentBalance * interestRate;

paymentMade = 1000.00;

currentBalance = interestPayment + currentBalance - paymentMade;

System.out.printf("Month %3d\t Payment $%8.2f New Balance $%8.2f\n",

month, paymentMade, currentBalance);

}

}

}

Month 1 Payment $ 1000.00 New Balance $24250.00

Month 2 Payment $ 1000.00 New Balance $23492.50

Month 3 Payment $ 1000.00 New Balance $22727.43

Month 4 Payment $ 1000.00 New Balance $21954.70

Month 5 Payment $ 1000.00 New Balance $21174.25

Month 6 Payment $ 1000.00 New Balance $20385.99

Month 7 Payment $ 1000.00 New Balance $19589.85

Month 8 Payment $ 1000.00 New Balance $18785.75

Month 9 Payment $ 1000.00 New Balance $17973.60

Month 10 Payment $ 1000.00 New Balance $17153.34

…….

Month 25 Payment $ 1000.00 New Balance $ 3817.60

Month 26 Payment $ 1000.00 New Balance $ 2855.78

Month 27 Payment $ 1000.00 New Balance $ 1884.33

Month 28 Payment $ 1000.00 New Balance $ 903.18

Month 29 Payment $ 1000.00 New Balance $ -87.79

What is

the

problem?

public class CarLoan {

public static void main(String a[]){

double currentBalance, interestRate, interestPayment, paymentMade;

int month = 0;

currentBalance = 25000.00;

interestRate = 0.01;

while (currentBalance > 0.0) {

month ++;

interestPayment = currentBalance * interestRate;

if (interestPayment + currentBalance >= 1000.00){

paymentMade = 1000.00;

currentBalance = interestPayment + currentBalance – paymentMade;

} else {

paymentMade = interestPayment + currentBalance;

currentBalance = 0.0;

}

System.out.printf("Month %3d\t Payment $%8.2f New Balance $%8.2f\n",

month, paymentMade, currentBalance);

}

}

}

Arithmetic expressions- Rules

•Precedence rules for arithmetic operations : Parenthesis first Multiplication, division and mod next Addition subtraction last

Associativity rules : Left to right for arithmetic operations

Type rules : If both operands are integers result is integer If any of the operands is floating point

result is floating

Assignment statement

X = expression

If LHS and RHS are both same type no

conversion

If LHS is floating and RHS is integer RHS

converted to floating

If LHS is integer and RHS is floating RHS

truncated to integer

The value of an assignment is the value

assigned => X = RHS has a value

Assignment statement

Problem :What will be printed by the following

program fragment ?

int x = 2, y = 5, z = 3, p, q, r;

float a;

p = (x + y*z) * 5 + 3/5 * 5;

r = q = 3.0/5 * 5;

a = (5 - 3) /3;

printf(“p = %d\n q = %d\n a = %f\n”, p, q, a);

Assignment statement

int x = 2, y = 5, z = 3, p, q, r;

p = (x + y*z) * 5 + 3/5 * 5;

Assignment statement

int x = 2, y = 5, z = 3, p, q, r;

p = (x + y*z) * 5 + 3/5 * 5;

85

Assignment statement

r = q = 3.0/5 * 5;

q = 3.0/5 * 5 is an assignment.

The value of an assignment is the value

assigned.

The assignment q = 3.0/5 * 5 has a value 3.0.

Thus r = q = 3.0/5 * 5 will assign 3.0 to both q

and r.

Note = is right associative !!

r = q = 3.0/5 * 5;

Conditions

• Relational operators : >, <, >=, <=, !=, ==.

• Logical operators : &&, ||, !

• Exercises:

• If count is 0 and limit is 10, what is the value

of

• (count == 0) && (limit < 20)

• count == 0 && limit < 20

• ! (count == 12)

• (limit < 0) && ((limit/count) > 7) Note why

this does not create a problem.