Sie sind auf Seite 1von 14

Object Oriented Programming (SWE-103) DATE:4th July 2018

Solved Assignment No.1 (05 marks)

1) How many keywords in the Java Language define shortly.


Ans.

Answer: abstract, assert, boolean, break, byte, case, catch, char,


class, const, continue, default, do, double, else, enum, extends,
false, final, finally, float, goto, for, if, implements, import,
instanceof, int, interface, long, native, new, null, package, private,
protected, public, return, short, static, strictfp.

2) Define Scope and lifetime of variables in the Java with an example.

 Java allows variables to be declared in any block. A block


defines a scope. Each time when we start a new block we are
starting a new scope.
 Scope determines what objects are visible to other parts of the
program and also determines the lifetime of those objects.
 Two major scopes in Java are
o Defined by a class.
o Defined by a method.
 The Scope defined by a method begins with its opening curly
brace. As a general rule, a variable declared inside a scope are
not visible to the code that is defined outside the scope.
 Example:
public class Scope
{
public static void main(String[] args)
{
int x; // known to all code within the main
x=10;

y=100; //error! y not known here

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

// x is still known here


System.out.println(“X=”+x);
}
}
3) Define Bitwise Logical Operator (left shift and Right shift) with an
example.
 >> (Signed right shift) In Java, the operator ‘>>’ is signed right shift operator. All
integers are signed in Java, and it is fine to use >> for negative numbers. The
operator ‘>>’ uses the sign bit (left most bit) to fill the trailing positions after shift.
If the number is negative, then 1 is used as a filler and if the number is positive,
then 0 is used as a filler. For example, if binary representation of number
is 10….100, then right shifting it by 2 using >> will make it 11…….1.
See following Java programs as example ‘>>’

class Test {
public static void main(String args[]) {
int x = -4;
System.out.println(x>>1);
int y = 4;
System.out.println(y>>1);
}
}

Output:
-2
2
 >>> (Unsigned right shift) In Java, the operator ‘>>>’ is
unsigned right shift operator. It always fills 0
irrespective of the sign of the number.
class Test {
public static void main(String args[]) {

// x is stored using 32 bit 2's complement form.


// Binary representation of -1 is all 1s (111..1)
int x = -1;

System.out.println(x>>>29); // The value of 'x>>>29' is


00...0111
System.out.println(x>>>30); // The value of 'x>>>30' is
00...0011

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

System.out.println(x>>>31); // The value of 'x>>>31' is


00...0001
}
}
The left shift operator, <<, simply shifts all of the bits in a
value to the left, a specified number of times. Here is the
general form to use left shift operator in Java:
Java Left Shift Operator Example
Here this program illustrates the concept of the left shift
operator :
/* Java Program Example - Java Left Shift
* Left shifting a byte value.
*/
public class JavaProgram
{
public static void main(String args[])
{

byte a = 64, b;
int i;

i = a << 2;
b = (byte) (a << 2);

System.out.println("Original value of a : " +a);


System.out.println("i is " + i + " and b is " + b);

}
}
Output:

4) Write a Java program that reads a number in inches, converts it to


meters.
/* Write a program that reads a number in feet, converts it to

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

meter, and display the result. One foot is 0.305 meter.*/

import java.util.Scanner;
class ConvertFeetIntoMeters{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter a value for feet: ");

double feet = input.nextDouble();


double meters = feet * 0.305;
// Format it to two decimal place
meters = (int)(meters * 100) / 100.0;

System.out.print(feet + " feet is " + meters + "


meteres");
}
}

5) Write a java program to generate a following @'s triangle.


@
@@
@@@
@@@@
@@@@@
@@@@@@
import java.util.Scanner;
public class Exercise24 {
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Input the number: ");
int n = in.nextInt();
for (int i = 0; i < n; i++)
{
for (int spc = n - i; spc > 0; spc--)
{
System.out.print (" ");
}
for (int j = 0; j <= i; j++)
{
System.out.print("@");
}
Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

System.out.println();
}
}
6) Write a program in Java to display the pattern like a diamond.
*
***
*****
*******
*********
***********
*************
***********
*********
*******
*****
***
*
import java.util.Scanner;
public class Diamond
{
public static void main(String args[])
{
int i,j,r;
System.out.print("Input number of rows (half of the diamond) : ");
Scanner in = new Scanner(System.in);
r = in.nextInt();
for(i=0;i<=r;i++)
{
for(j=1;j<=r-i;j++)
System.out.print(" ");
for(j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.print("\n");
}

for(i=r-1;i>=1;i--)
{
for(j=1;j<=r-i;j++)
System.out.print(" ");
for(j=1;j<=2*i-1;j++)
System.out.print("*");
System.out.print("\n");
}
}
}
Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

7) Write a Java program to find the number of days in a month.


e.g Input a month number: 2
Input a year: 2018
February 2018 has 28 days.
import java.util.Scanner;
public class Exercise7
{
public static void main(String[] strings)
{
Scanner input = new Scanner(System.in);
int number_Of_DaysInMonth = 0;
String MonthOfName = "Unknown";
System.out.print("Input a month number: ");
int month = input.nextInt();
System.out.print("Input a year: ");
int year = input.nextInt();
switch (month) {
case 1:
MonthOfName = "January";
number_Of_DaysInMonth = 31;
break;
case 2:
MonthOfName = "February";
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) {
number_Of_DaysInMonth = 29;
} else {
number_Of_DaysInMonth = 28;
}
break;
case 3:
MonthOfName = "March";
number_Of_DaysInMonth = 31;
break;
case 4:
MonthOfName = "April";
number_Of_DaysInMonth = 30;
break;
case 5:
Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

MonthOfName = "May";
number_Of_DaysInMonth = 31;
break;
case 6:
MonthOfName = "June";
number_Of_DaysInMonth = 30;
break;
case 7:
MonthOfName = "July";
number_Of_DaysInMonth = 31;
break;
case 8:
MonthOfName = "August";
number_Of_DaysInMonth = 31;
break;
case 9:
MonthOfName = "September";
number_Of_DaysInMonth = 30;
break;
case 10:
MonthOfName = "October";
number_Of_DaysInMonth = 31;
break;
case 11:
MonthOfName = "November";
number_Of_DaysInMonth = 30;
break;
case 12:
MonthOfName = "December";
number_Of_DaysInMonth = 31;
}
System.out.print(MonthOfName + " " + year + " has " +
number_Of_DaysInMonth + " days\n");
}
}
8) Write a Java program to solve quadratic equations (use if, else if and
else) and display the resultant is positive or negative.
import java.util.Scanner;
public class Quadratic

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

{
public static void main(String[] Strings)
{
Scanner input = new Scanner(System.in);
System.out.print("Input a: ");
double a = input.nextDouble();
System.out.print("Input b: ");
double b = input.nextDouble();
System.out.print("Input c: ");
double c = input.nextDouble();
double result = b * b - 4.0 * a * c;
if (result > 0.0) {
double r1 = (-b + Math.pow(result, 0.5)) / (2.0 * a);
double r2 = (-b - Math.pow(result, 0.5)) / (2.0 * a);
System.out.println("The roots are " + r1 + " and " + r2+"The
root result is positive");
} else if (result == 0.0) {
double r1 = -b / (2.0 * a);
System.out.println("The root is " + r1);
} else {
System.out.println("The equation has no real
roots \nThe root result is Negative.");
}

}
}
9) Write a Java program to create Calculator by using Switch statement.
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
System.out.print("Enter two numbers: ");
// nextDouble() reads the next double from the keyboard
double first = reader.nextDouble();
double second = reader.nextDouble();
System.out.print("Enter an operator (+, -, *, /): ");
char operator = reader.next().charAt(0);
double result;

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

switch(operator)
{
case '+':
result = first + second;
break;

case '-':
result = first - second;
break;

case '*':
result = first * second;
break;

case '/':
result = first / second;
break;

// operator doesn't match any case constant (+, -, *, /)


default:
System.out.printf("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second,
result);
}
}
10)Define Jump Statement(break, continue, and return)with and
example.
Java Jump Statements
Jump statements are used to interrupt the normal flow of program.

Types of Jump Statements are:

 break
 continue
 labelled loop

break statement
Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

break statement is used inside loop or switch statement. When compiler finds the
break statement inside a loop, compiler will abort the loop and continue to execute
statements followed by loop.

Example of break statement

//BreakStatementDemo.java

class BreakStatementDemo
{
public static void main(String args[])
{
int a=1;

while(a<=10)
{
if(a==5)
break;

System.out.print("\n\tStatement : " + a);


a++;

System.out.print("\n\tEnd of Program.");
}
}

Output :

Statement 1.
Statement 2.
Statement 3.
Statement 4.
End of Program.
continue statement
continue statement is also used inside loop. When compiler finds the break
statement inside a loop, compiler will skip all the followling statements in the loop
and resume the loop.
Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

Example of continue statement


//ContinueStatementDemo.java

class ContinueStatementDemo
{
public static void main(String args[])
{
int a=0;
while(a<10)
{
a++;
if(a==5)
continue;
System.out.print("\n\tStatement " + a);
}
System.out.print("\n\tEnd of Program.");
}
Output :
Statement 1.
Statement 2.
Statemnet 3.
Statement 4.
Statement 6.
Statement 7.
Statement 8.
Statement 9.
Statement 10.
End of Program.
Lable Loop:

According to nested loop, if we put break statement in inner loop,


compiler will jump out from inner loop and continue the outer loop
again. What if we need to jump out from the outer loop using break
statement given inside inner loop? The answer is, we should
define lable along with colon(:) sign before loop.

Example without labelled loop


//WithoutLabelledLoop.java

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

class WithoutLabelledLoop
{
public static void main(String args[])
{
int i,j;

for(i=1;i<=10;i++)
{
System.out.println();

for(j=1;j<=10;j++)
{
System.out.print(j + " ");

if(j==5)
break; //Statement 1
}
}
}
}

Output :
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5
1 2 3 4 5

In th above example, statement 1 will break the inner loop and jump outside from
inner loop to execute outer loop.

Example with labelled loop


//WithLabelledLoop.java
class WithLabelledLoop
Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

{
public static void main(String args[])
{
int i,j;

loop1:
for(i=1;i<=10;i++)
{
System.out.println();
loop2:
for(j=1;j<=10;j++)
{
System.out.print(j + " ");
if(j==5)
break loop1; //Statement 1
}
}
}
}

Output : 1 2 3 4 5

In th above example, statement 1 will break the inner loop and jump outside the
outer loop.

The return statement


The return statement is the last jump statement. The return statement
is used to end the execution of a specific method and then return a
value. When we use a return statement in our program then it sends the
program control to the method caller. The data type of the returned
value should always be equal to the data type of the method's declared
return value.
Syntax :
if(condition)
{
return;
}
Example :
package myclass1;
class Myclass1

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.
Object Oriented Programming (SWE-103) DATE:4th July 2018

{
public static void main(String[] args)
{
yahoo(true);
System.out.println("hi");
}
public static void yahoo(boolean a)
{
System.out.println("1");
if (a)
{
return;
}
System.out.println("2");
System.out.println("3");
}
}

Output :
1
hi

Note: Submission Date is 23rd July 2018 and required handwritten assignment
with separate file, Late assignment will not be acceptable. Attached this
Question Paper in your assignment file.

Engr.Sumreena Bano,
Lecturer,
Computer Engineering Department.

Das könnte Ihnen auch gefallen