Course Hero Logo

lab no 2 tass oop.pdf - 0BJECT ORIENTED PROGRAMMING...

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 1 - 5 out of 12 pages.

0BJECT ORIENTEDPROGRAMMINGSUBMITTED TO ENGRINEER HAMID SAEEDTASSADUQ RASHEEDFA19-BEE-040LAB NO 02:Object Oriented Modeling & Control Structures
0BJECT ORIENTED PROGRAMMINGTASSADUQ RASHEED1EXERCISE TASK 1:Write a program that specifies whether a given number (x) falls in one of the following categories (give x a valuefrom the code, don't read from user):• 0 to 9• 10 to 19• 20 to 29None of the categoriesFor example, if x = 5, program should print "0 to 10", and if x = 44 it should print "None".PROGRAM1.packagetask1;2.3.public classTask1{4.5.6.public static voidmain(String[]args) {7.intx=15;8.if(x>=0&&x<=9){9.System.out.println("0 to 9");10.}11.else if(x>=10&&x<=19){12.System.out.println("10 to 19");13.}14.else if(x>=20&&x<=29){15.System.out.println("20 to 29");16.}17.else{18.System.out.println("none");19.}20.}21.}OUTPUTrun:10to19BUILD SUCCESSFUL(total time:0seconds)EXERCISE TASK 2:
0BJECT ORIENTED PROGRAMMINGTASSADUQ RASHEED2Write a program that reads two integers and prints their sum like the code below (text shown in boldface issupposed to be user input). Enter the first number: 3Enter the second number: 4The sum is 7PROGRAM1.packagetask2;2.3.importjava.io.*;4.public classTask2{5.6.public static voidmain(String[]args)7.throwsIOException{8.BufferedReaderin;9.in=newBufferedReader(newInputStreamReader(System.in));10.intx,y;11.StringuserInput;12.System.out.print("enter a number");13.userInput=in.readLine();14.x=Integer.parseInt(userInput);15.System.out.print("enter a number");16.userInput=in.readLine();17.y=Integer.parseInt(userInput);18.System.out.println(x+y);19.}20.21.}OUTPUTrun:enter a number322enter a number32354BUILD SUCCESSFUL(total time:11seconds)EXERCISE TASK 3:Write a program that asks the user to enter two numerical values (integers) and then select an operation (addition,subtraction, multiplication and division) then prints the result based on operation selected. The code below showsexamples of the output (text shown in boldface is supposed to be user input). Enter first number: 4 Enter secondnumber: 2 1. Addition (+).
0BJECT ORIENTED PROGRAMMINGTASSADUQ RASHEED32. Subtraction (-).

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 12 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Winter
Professor
N/A
Tags
Integers, Negative and non negative numbers, TASSADUQ RASHEED

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture