java programming: elementary practice

38
Structure programming Structure programming Java Programming – Practice Java Programming – Practice part -1 part -1 Faculty of Physical and Basic Education Computer Science By: Msc. Karwan M. Kareem 2013 - 2014 1 aculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Upload: karwan-mustafa-kareem

Post on 24-Jul-2015

214 views

Category:

Technology


8 download

TRANSCRIPT

Page 1: Java programming: Elementary practice

Structure programming Structure programming Java Programming – PracticeJava Programming – Practice

part -1part -1

Faculty of Physical and Basic Education

Computer Science By: Msc. Karwan M.

Kareem2013 - 2014

1© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 2: Java programming: Elementary practice

Installing JDKInstalling JDK1- go to JDK folder

2- Right click on JDK icon and click open

2© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 3: Java programming: Elementary practice

3- Waiting for configuring the installer ..

3© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 4: Java programming: Elementary practice

4- click on next button..

4© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 5: Java programming: Elementary practice

5- choose the installation folder..

6 – click on next button …

5© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 6: Java programming: Elementary practice

7 – click on Install button …

6© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 7: Java programming: Elementary practice

8 – Waiting for installing all JDK files…

7© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 8: Java programming: Elementary practice

8 – Installation completed successfully and click on finish button to finish the JDK…

8© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 9: Java programming: Elementary practice

1- Click on the Eclipse – Shortcut or Eclipse -Icon

2- Choose a workspace folder to use for this session

3- Click ok

Starting to write first Java program ....

9© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 10: Java programming: Elementary practice

4- go to file list new Java project

5- write your project name

10© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 11: Java programming: Elementary practice

6- click next button

7- click finish

11© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 12: Java programming: Elementary practice

8- go to the project right click new class

9- write name for your class

10- choose the modifier “public”

12© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 13: Java programming: Elementary practice

11- choose the method

12- click finish

13© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 14: Java programming: Elementary practice

13- write your Java Program

14- click on run and get the result

14© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 15: Java programming: Elementary practice

1- Input: Define all variables

1- process

1- Output: print results

Example: Write a Java program to calculate two numbers Example: Write a Java program to calculate two numbers

15© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 16: Java programming: Elementary practice

Example: Write a Java program to compute the area of the circle

1- allocate memory for radius

2- allocate memory for area

3- compute area and assign it to variable area 4- print a

message to the console

16© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 17: Java programming: Elementary practice

Write a Java program to compute the area of the circle ( use JOptionPane)

Page 18: Java programming: Elementary practice

Example: Write a Java program to print out full name, with use the Example: Write a Java program to print out full name, with use the scanner classscanner class to handle input from a user. to handle input from a user.

import java.util.Scanner;import java.util.Scanner;public class FullName {public class FullName {

public static void main(String[] args) {public static void main(String[] args) {// TODO Auto-generated method stub// TODO Auto-generated method stub

Scanner In = new Scanner (System.Scanner In = new Scanner (System.in);in);

System.System.out.println("enter first name:");out.println("enter first name:"); String first_name = In.next();String first_name = In.next();

System.System.out.println("enter last name");out.println("enter last name"); String last_name=In.next();String last_name=In.next();

System.System.out.println("Your full name is: " + first_name +" "+ last_name);out.println("Your full name is: " + first_name +" "+ last_name); } }} }

Create new Scanner Object

Read first input

Read second input

18© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 19: Java programming: Elementary practice

Example: Write a Java program to print out your profile, Example: Write a Java program to print out your profile, with use the with use the scanner class scanner class to handle input from a userto handle input from a user

import java.util.Scanner;public class FullName {

public static void main(String[] args) {// TODO Auto-generated method stub

Scanner In = new Scanner (System.in);

System.out.println("enter first name:"); String first_name = In.next();

System.out.println("enter last name "); String last_name=In.next();

System.out.println("enter your age "); int age=In.nextInt();

System.out.println("enter your adress "); String address=In.next();

System.out.println("enter your gender "); String gender=In.next(); System.out.println("Your full name is: " + first_name + " " + last_name); System.out.println("Your age is: " + age); System.out.println("Your address: " + address); System.out.println("Your gender is: " + gender); } }

Create new Scanner Object

Read data from consol

Print all variables

19© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 20: Java programming: Elementary practice

Example: Write a program that obtains Current Date & Time

This is very easy to get current date and time in Java. You can use a simple Date object with toString() method to print current date and time as follows:

import java.util.Date; public class DateDemo { public static void main(String args[]) {

// Instantiate a Date object Date date = new Date();

// display time and date using toString() System.out.println(date.toString()); } }

Create new Date Object

Using toString to print current date and time

20© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 21: Java programming: Elementary practice

)94

(9))(5(10

5

43

y

x

xx

cbayx

Example: Write a JAVA program to find out the output of the Example: Write a JAVA program to find out the output of the following rulesfollowing rules

import java.util.Scanner;

public class arth {public static void main(String[] args) { Scanner In = new Scanner (System.in);

System.out.println("enter number a "); int a=In.nextInt();

System.out.println("enter number b "); int b=In.nextInt();

System.out.println("enter number c "); int c=In.nextInt();

System.out.println("enter number x "); int x=In.nextInt();

System.out.println("enter number y "); int y=In.nextInt();

double outPut = (3 + 4 * x) / 5 - (10 * (y-5)*(a+b+c))/x +9*(4/x+(9+x/y));

System.out.println("the result is :" + outPut); } }

Create new Scanner Object

Read data from consol

21© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 22: Java programming: Elementary practice

Question 1: Write a Java program to compute the area of the Trapezium, with use the scanner class to handle input from a user, and JOptionPane to print the result.

22© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 23: Java programming: Elementary practice

Question 2Question 2: Write a Java program to compute the area of the : Write a Java program to compute the area of the SectorSector, , with use the with use the scanner classscanner class to handle input from a user, to handle input from a user, and and JOptionPaneJOptionPane to print the result. to print the result.

23© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 24: Java programming: Elementary practice

Question 3Question 3: Write a Java program to compute the area of the : Write a Java program to compute the area of the EllipseEllipse, , with use the with use the scanner classscanner class to handle input from a user, to handle input from a user, and and JOptionPaneJOptionPane to print the result. to print the result.

24© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 25: Java programming: Elementary practice

Question 4:Question 4: Write a JAVA program to find the output of the Write a JAVA program to find the output of the following rules.following rules.

- Use Use Scanner classScanner class to handle input from a user. to handle input from a user.- Use Use JOptionPaneJOptionPane to print the result. to print the result.

)4

(9))(5(10

5

4

h

L

LL

cbahLy

mh

k

dk

hkb

4)5(11

5

15

25© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 26: Java programming: Elementary practice

Question 5: Question 5: Write a Java program to print out your profile, Write a Java program to print out your profile, with use the with use the scanner class scanner class to handle input from a userto handle input from a user

outputoutputName: Karwan M. KareemName: Karwan M. KareemGender: MaleGender: MaleAge: 30Age: 30Email address: [email protected] address: [email protected]: SulAddress: SulNote: any thing Note: any thing

26© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 27: Java programming: Elementary practice

Question 6: Write a java program to find the Question 6: Write a java program to find the SumSum and and AverageAverage of five lessons, and print them out with your of five lessons, and print them out with your name. is he/ she good or bad student (Use JOptionPane for name. is he/ she good or bad student (Use JOptionPane for Input)Input)

OUTPUT :OUTPUT : Your name : Karwan M. KareemYour name : Karwan M. Kareem Lessons1=55Lessons1=55 Lessons2=55Lessons2=55 Lessons3=55Lessons3=55 Lessons4=55Lessons4=55 Lessons5=55Lessons5=55

The sum = 275The sum = 275 The average = 55The average = 55 He is goodHe is good

27© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 28: Java programming: Elementary practice

28© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 29: Java programming: Elementary practice

Question 7: Write a Java program to compare three numbers Question 7: Write a Java program to compare three numbers and find largest out of three (Use JOptionPane).and find largest out of three (Use JOptionPane).

X greater then y and x greater then z X greater then y and x greater then z "First number is largest" "First number is largest"Y greater then x and y greater then z Y greater then x and y greater then z "Second number is largest" "Second number is largest"z z greater thengreater then x x and and z z greater thengreater then y y "Third number is largest" "Third number is largest" Else Else "Entered numbers are equals..." "Entered numbers are equals..."

Question 7: Write a Java program to compare three numbers Question 7: Write a Java program to compare three numbers and find largest out of three (Use JOptionPane).and find largest out of three (Use JOptionPane).

X greater then y and x greater then z X greater then y and x greater then z "First number is largest" "First number is largest"Y greater then x and y greater then z Y greater then x and y greater then z "Second number is largest" "Second number is largest"z z greater thengreater then x x and and z z greater thengreater then y y "Third number is largest" "Third number is largest" Else Else "Entered numbers are equals..." "Entered numbers are equals..."

29© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 30: Java programming: Elementary practice

Question 8: write the program in Java about Question 8: write the program in Java about if elseif else the program the program should ask the user to enter the total marks out of 100, and thenshould ask the user to enter the total marks out of 100, and then display the letter grade on the screen (Use Scanner class)display the letter grade on the screen (Use Scanner class)

* Grade Rules:* Grade Rules:

0-49 => D grad 0-49 => D grad 50-55 => C grad 50-55 => C grad 56-60 => C+ grad 56-60 => C+ grad 61-70 => B grad 61-70 => B grad 71-75 => B+ grad 71-75 => B+ grad 76-85 => A grad 76-85 => A grad 86-100 => A+ grad 86-100 => A+ grad

Question 8: write the program in Java about Question 8: write the program in Java about if elseif else the program the program should ask the user to enter the total marks out of 100, and thenshould ask the user to enter the total marks out of 100, and then display the letter grade on the screen (Use Scanner class)display the letter grade on the screen (Use Scanner class)

* Grade Rules:* Grade Rules:

0-49 => D grad 0-49 => D grad 50-55 => C grad 50-55 => C grad 56-60 => C+ grad 56-60 => C+ grad 61-70 => B grad 61-70 => B grad 71-75 => B+ grad 71-75 => B+ grad 76-85 => A grad 76-85 => A grad 86-100 => A+ grad 86-100 => A+ grad

30© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 31: Java programming: Elementary practice

Question 9: Write a java program to implement a Vote System. (Use JOptionPane)

Name : karwan kareem address: Sul / Hilan City gender: Male age: 30

Age > 0 and age <18print “cannot vote”age > 18 and age < 100 print “can vote “else print “you have error ”

Question 9: Write a java program to implement a Vote System. (Use JOptionPane)

Name : karwan kareem address: Sul / Hilan City gender: Male age: 30

Age > 0 and age <18print “cannot vote”age > 18 and age < 100 print “can vote “else print “you have error ”

31© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 32: Java programming: Elementary practice

Question 10: Write a java program about “Good student “(use JOptionPane)

If gender is male and grade is 70 and above: display “Good boy"If gender is female and grade is 70 above: display “Good girl"but if grade is below 70 regardless of gender: display “Bad child"

Question 10: Write a java program about “Good student “(use JOptionPane)

If gender is male and grade is 70 and above: display “Good boy"If gender is female and grade is 70 above: display “Good girl"but if grade is below 70 regardless of gender: display “Bad child"

32© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 33: Java programming: Elementary practice

Question 11: write a java program to create “size-system” (Use JOptionPane).

If size equal to 1 X-smallIf size equal to 2 smallIf size equal to 3 Medium If size equal to 4 large If size equal to 5 X-large

Question 11: write a java program to create “size-system” (Use JOptionPane).

If size equal to 1 X-smallIf size equal to 2 smallIf size equal to 3 Medium If size equal to 4 large If size equal to 5 X-large

33© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 34: Java programming: Elementary practice

Question 12: write the output of the following Java codes. public class karwan {

public static void main(String[] args) {long L = 1023; long M = 1024;long N = 1025;if ( M> L) {

if (M >N)System.out.println(L++);

else System.out.println(M++);

} else if (M != N) if (M != L)

System.out.println(--N);else

System.out.println(--M);} }

Question 12: write the output of the following Java codes. public class karwan {

public static void main(String[] args) {long L = 1023; long M = 1024;long N = 1025;if ( M> L) {

if (M >N)System.out.println(L++);

else System.out.println(M++);

} else if (M != N) if (M != L)

System.out.println(--N);else

System.out.println(--M);} }

34© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 35: Java programming: Elementary practice

Question 13: write the output of the following Java codes. public class karwanTest {

public static void main(String[] args) {double H = 1000.0; double I = 1001.0;double J = 1002.0;boolean K = (J > H); double L=0.0; if ( I< H) {

if (J >I)System.out.println( J-=10);

else System.out.println(I*=10);

} else if (J != H)if (J == I)

System.out.println(K);else

L=J%2;System.out.println(L+=10.0);

} }

Question 13: write the output of the following Java codes. public class karwanTest {

public static void main(String[] args) {double H = 1000.0; double I = 1001.0;double J = 1002.0;boolean K = (J > H); double L=0.0; if ( I< H) {

if (J >I)System.out.println( J-=10);

else System.out.println(I*=10);

} else if (J != H)if (J == I)

System.out.println(K);else

L=J%2;System.out.println(L+=10.0);

} }35© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 36: Java programming: Elementary practice

Question 14: write a java program to find the square root of Question 14: write a java program to find the square root of the number between 20 and 40 (Use scanner class)the number between 20 and 40 (Use scanner class)

import static java.lang.Math.*;

double sq = Math.sqrt(25)double sq = Math.sqrt(25)

20 – 40 20 – 40 print the square root of the number print the square root of the numberElse Else print “you enter unknown number”print “you enter unknown number”

36© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 37: Java programming: Elementary practice

0

10

20

30

40

50

60

70

200

B[0]

B[1]

B[2]

B[3]

B[4]

B[5]

B[6]

B[7]

B[20]

Input output0

1

2

3

4

5

6

7

20

process

Question 15: write a java program to print out the following arrays.

Sum

210

37© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015

Page 38: Java programming: Elementary practice

0

10

20

30

40

50

60

70

200

B[0]

B[1]

B[2]

B[3]

B[4]

B[5]

B[6]

B[7]

B[20]

Input output0

- 10

- 20

- 30

- 40

- 50

- 60

- 70

- 20

process

Question 16: Question 16: write a java program to print out the following arrays.write a java program to print out the following arrays.

Sum

-2100

38© University of Sulaimani, Faculty of Physical & Basic Education, Department of Computer Science 2014 / 2015