You are on page 1of 11

ARNAV ADARSH

209301591
LAB 3& 4

Q1. Write a program in java to take 10 integer numbers as


user input using the BufferedReader and print the sum of these
numbers.

CODE
import java.io.*;
class Example1
{
public static void main(String args[]) throws
IOException
{
int number = 0;
int sum = 0;

BufferedReader br = new BufferedReader(new


InputStreamReader(System.in));

for(int i =0; i < 10; i++)


{
System.out.println("Please Enter " + i +
" Number");
// Reads the Number using ‘br’ instance
field
number = Integer.parseInt(br.readLine());
sum += number;
}
System.out.println("Sum =" + sum);
System.out.println("Arnav Adarsh, 209301591, sec
F");

}
}
OUTPUT
EXAMPLE 2.
Write a program in java which reads the values of five
attributes related with a student such as name (name of a
student), age (age of a student), marks_in_phy (marks in
physics out of 100), marks_in_chem(marks in chemistry out of
100) and marks_in_math(marks in mathematics out of 100). In the
end program displays the average marks and final grade of the
student. The final grade of the student is computed as
follows:1.Final Grade = “A++” if average marks >=902.Final Grade
= “A+” if average marks <90 but >= 803.Final Grade = “A” if
average marks <80 but >= 70 4.Final Grade = “B++” if average
marks < 70 but >= 605.Final Grade = “B+” if average marks <60 but
>= 506.Final Grade = “B” if average marks <50 but >= 407.Final
Grade = “C” if average marks < 40 but >=358.Final Grade = “Fail” if
average marks < 35 An Incomplete Program is given below and you
have to complete it.

CODE
import java.io.*;
class Student {
public static void main(String []args) throws
IOException
{
System.out.println("Arnav
Adarsh\n209301361\nSection F");
BufferedReader br = new BufferedReader(new
InputStreamReader(System.in));
String name;
int age;
int marks_in_phy,marks_in_chem,marks_in_maths;
System.out.println("Enter name, age, marks in
physics, chemistry and maths of a student");
name = br.readLine();
age = Integer.parseInt(br.readLine());
marks_in_phy=Integer.parseInt(br.readLine());
marks_in_chem=Integer.parseInt(br.readLine());
marks_in_maths=Integer.parseInt(br.readLine());
double avg =
(marks_in_chem+marks_in_maths+marks_in_phy)/3.0;
System.out.println("Average marks : "+avg);
System.out.print("Grade : ");
if(avg>=90) {
System.out.println("A++");
} else if(avg>=80) {
System.out.println("A+");
} else if(avg>=70) {
System.out.println("A");
} else if(avg>=60) {
System.out.println("B++");
} else if(avg>=50) {
System.out.println("B+");
} else if(avg>=40) {
System.out.println("B");
} else if(avg>=35) {
System.out.println("C");
} else {
System.out.println("Fail");
}
System.out.println("Arnav Adarsh, 209301591, sec
F");

}
}

OUTPUT
EXAMPLE 3
Rewrite the Code given in Exercise 1 Using Scanner class
and these programs are to recorded in the Lab file in handwritten
form.

CODE
import java.util.Scanner;

public class Example3 {


public static void main(String args[])
{
int number = 0;
int sum = 0;

Scanner br = new Scanner(System.in);

for(int i =0+1; i < 10; i++)


{
System.out.println("Please Enter " + i + "
Number");
// Reads the Number using ‘br’ instance field
//number = Integer.parseInt(br.readLine());
number=br.nextInt();
sum += number;
}
System.out.println("Sum =" + sum);
System.out.println("Arnav Adarsh, 209301591, sec
F");

}
}
EXAMPLE 4.
Rewrite the Code given in Exercise 2 Using Scanner class
and these programs are to recorded in the Lab file in handwritten
form.

CODE
import java.util.Scanner;

class Example4 {
public static void main(String []args)
{
System.out.println("Arnav
Adarsh\n209301361\nSection F");
Scanner br = new Scanner(System.in);
String name;
int age;
int marks_in_phy,marks_in_chem,marks_in_maths;
System.out.println("Enter name, age, marks in
physics, chemistry and maths of a student");
name = br.nextLine();
age = br.nextInt();
marks_in_phy=br.nextInt();
marks_in_chem=br.nextInt();
marks_in_maths=br.nextInt();
double avg =
(marks_in_chem+marks_in_maths+marks_in_phy)/3.0;
System.out.println("Average marks : "+avg);
System.out.print("Grade : ");
if(avg>=90) {
System.out.println("A++");
} else if(avg>=80) {
System.out.println("A+");
} else if(avg>=70) {
System.out.println("A");
} else if(avg>=60) {
System.out.println("B++");
} else if(avg>=50) {
System.out.println("B+");
} else if(avg>=40) {
System.out.println("B");
} else if(avg>=35) {
System.out.println("C");
} else {
System.out.println("Fail");
}
System.out.println("Arnav Adarsh, 209301591, sec
F");

}
}
EXAMPLE 5
Define a class named ‘Point’ which encapsulates a point in 2-
dimensional space. A point ‘p’ is represented by two co-ordinates
name ‘x’ (x-coordinate) and ‘y’ (y-coordinate). You have to define
the class as per following specification

Add two attributes in class ‘Point’ named ‘x’ and ‘y’ of type double
and having class scope

Add two methods for reading/getting the values of ‘x’ and ‘y’ fields

Add methods for setting/updating the values of ‘x’ and ‘y’ field

Add a method for checking the equality of any two‘Point’ type


instances. Suppose ‘p1’ and ‘p2’ are two Point type instances. They
are equal iff the values their ‘x’ and ‘y’ fields are equal

Add a method to compute the distance between two points.

Add a method to display the details of any point type instance.


Define a driver class named ‘PointTest’ which performs the
following tasks sequentially

Create two ‘Point’ type instances named ‘p1’ and ‘p2’.

Display the details of p1 and p2

Update the values of ‘x’ and ‘y’ fields of instance ‘p1’ to 3.4 and 5.6
respectively

Update the values of ‘x’ and ‘y’ fields of instance ‘p2’ to 13.4 and
15.6 respectivelyCompute the distance between points ‘p1’ and
‘p2’

CODE
import java.util.Scanner;

class Point {
private double x;
private double y;
public double getX() {
return x;
}
public double getY() {
return y;
}
public void setX(double x) {
this.x=x;
}
public void setY(double y) {
this.y=y;
}
public boolean isEqual(Point p2) {
return (x==p2.x && y==p2.y);
}
public double computeDistance(Point p2) {
double a=y-p2.y;
double b=x-p2.x;
a=a*a;
b=b*b;
double dis=a+b;
dis=Math.sqrt(dis);
return dis;
}
public void display() {
System.out.println("Point : ("+x+", "+y+")");
}
}
class PointTest {
public static void main(String []args) {
Point p1=new Point();
Point p2=new Point();
p1.display();
p2.display();
p1.setX(3.4);
p1.setY(5.6);
p2.setX(13.4);
p2.setY(15.6);
System.out.println("Updated points ");
p1.display();
p2.display();
double dis=p1.computeDistance(p2);
System.out.println("Distance between points
p1 and p2 : "+dis);
System.out.println("Arnav Adarsh, 209301591,
sec F");
}
}

You might also like