You are on page 1of 5

Department of Electronics & communication Engineering

Lab Report 01
Course Information
Course Title: Object Oriented Programming
Course code: ICE107
Section: 01
Course Instructor: Mohammad Rafsun Islam
Student Information
Name: Rafat Hasan Dipu
ID: 2020-1-50-042

Submitted to: Submitted by:


Mohammad Rafsun Islam Rafat Hasan Dipu

Date: 21-07-2020
Question 01: Write a java program which will take number of days as
an integer value and the program will produce output as the day of the
week after the inputted number of days from the current day. For
example, if the input is 6 and today is Friday, the program will produce
output “The weekday is Thursday”.

Code:
package helloworld;
import java.util.*;
public class Dayprint {

public static void main(String[] args) {


int day;
System.out.println("1.Saturday"
+ "\n2.Sunday"
+ "\n3.Monday"
+ "\n4.Tuesday"
+ "\n5.Wednesday"
+ "\n6.Thursday"
+ "\n7.Friday");
System.out.println("Enter value of number of days: ");
Scanner input = new Scanner(System.in);
day=input.nextInt();
switch(day) {
case 1:
System.out.println("The weekday is Staurday");
break;
case 2:
System.out.println("The weekday is Sunday");
break;
case 3:
System.out.println("The weekday is Monday");
break;
case 4:
System.out.println("The weekday is Tuesday");
break;
case 5:
System.out.println("The weekday is Wednesday");
break;
case 6:
System.out.println("The weekday is Thursday");
break;
case 7:
System.out.println("The weekday is Friday");
break;
default :
System.out.println("Input is invalid");
break;
}

Output:

Question 02: Write a java program that prints all real solutions to the
quadratic equation ax2+bx+c=0.Read in a, b, c and use the quadratic
formula. Quadratic formulas are x1= (-b+√b^2-4ac)/2a
X2= (-b-√b^2-4ac)/2a.

Code:
package helloworld;
import java.util.*;
public class Quadraticequation {

public static void main(String[] args) {


double a,b,c;
System.out.println("Enter the value of a:");
Scanner input =new Scanner (System.in);
a=input.nextDouble();
if(a==0)
{
System.out.println("Please enter the corect value of a:");
a=input.nextDouble();
}
System.out.println("Please enter the number of b and c:");
b=input.nextDouble();
c=input.nextDouble();
double d,x1,x2;
d=Math.sqrt(b*b-4*a*c);
if(d>0)
{
x1=((-b+d)/2*a);
x2=((-b-d)/2*a);
System.out.println("X1 value is= " + x1 );
System.out.println("X2 value is= " + x2 );
}
else
{
System.out.println("The values is undfined");
}

}
Output:
Discussion:

First java program is mainly show weekday. Now I explain that code.
In this program, when you run it, the program need a value. This portion
work based on mainly import java.util.*; . Now I explain main function.
Here we declare a integer type variable called day. Then I show 7 days
name by using print function. Then scanner class is used to get user
input, and it is found in the java.util.*; package. The user input assigned
in day variable. Then I use switch statement and its under 7 cases. When
user input value of “day” variable switch statement evaluates expression
and compares it values of 7 cases label. Now, if the value matches a
certain case label, then all the statements of the matching case  label are
executed and break the case. If value do not match any cases then code
go to default and break this.

Second java program is solving a quadratic equation ax2 + bx + c = 0.


First code I explain how to take user input. So, in this code I avoid this.
Now I explain main function. In this program I take six double type
variables. Then I check “a”. if “a” is zero then user get warning to give
correct value. Then program get value and it go to this line
“d=Math.sqrt(b*b-4*a*c);”. Here root calculation assign in d variable.
Then program check “d>0” because root cannot works negative
numbers. If “d” greater than zero then code execute if block. That’s a
normal calculation in if block. Then program print value of two roots
values of quadratic equation which are X1 and X2.

You might also like