1. create a class called student having data member like name,enr,branch,city,spi
define construct which can ini data member
define method for follwing func
get()
disp()
import java.util.Scanner;
class Student
{
int enr;
String name,city,branch;
double spi;
Student()
{
enr=123;
name="sam";
city="Surat";
branch="computer";
spi=7.21;
}
void get()
{
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter the Student Name: ");
name=sc.nextLine();
System.out.print("\nEnter the Student Branch: ");
branch=sc.nextLine();
System.out.print("\nEnter the Student City: ");
city=sc.nextLine();
System.out.print("\nEnter the Student Enrollement no: ");
enr=sc.nextInt();
System.out.print("\nEnter the Student SPI: ");
spi=sc.nextDouble();
}
void display()
{
System.out.println("Student Enrollement: "+enr);
System.out.println("Student Name: "+name);
System.out.println("Student Branch: "+branch);
System.out.println("Student City: "+city);
System.out.println("Student SPI: "+spi);
}
public static void main(String ar[])
{
Student std=new Student();
std.get();
std.display();
}
}
Output
2. It is required to compute SPI (semester performance index) of n students of your college for their registered subjects in a semester.
Declare a class called student having following data members: id_no ,no_of_subjects_registered ,subject_code , subject_credits ,grade_obtained and spi.
- Define constructor and calculate_spi methods.
- Define main to instantiate an array for objects of class student to process data of n students to be inputted through keyboard.
import java.util.Scanner;
class Studentspi
{
int id_no;
int subject_code[]={36001,36002,36003,36004};
int subject_credit[]={5,6,8,4};
String grade[]=new String[4];
int marks=0;
double spi;
String sub[]={"ADA","SP","JAVA","MPI"};
Studentspi()
{
Scanner s1=new Scanner(System.in);
int i;
System.out.print("Enter Student IDNO:");
this.id_no=s1.nextInt();
for(i=0;i<4;i++)
{
System.out.print("Enter grade of "+sub[i]+": ");
this.grade[i]=s1.next();
if(this.grade[i].equals("aa"))
{
this.marks=this.marks+(100*subject_credit[i]);
}
else if(this.grade[i].equals("ab"))
{
this.marks=this.marks+(90*subject_credit[i]);
}
else if(this.grade[i].equals("bb"))
{
this.marks=this.marks+(80*subject_credit[i]);
}
else if(this.grade[i].equals("bc"))
{
this.marks=this.marks+(70*subject_credit[i]);
}
else if(this.grade[i].equals("cc"))
{
this.marks=this.marks+(60*subject_credit[i]);
}
else if(this.grade[i].equals("cd"))
{
this.marks=this.marks+(50*subject_credit[i]);
}
else if(this.grade[i].equals("dd"))
{
this.marks=this.marks+(40*subject_credit[i]);
}
}
}
void calculate_spi()
{
this.spi=(this.marks/23)/10;
System.out.println("Student Id_no:"+this.id_no);
System.out.println("");
System.out.println("Subject_code Subject Subject_Credit Grade");
for(int i=0;i<4;i++)
{
System.out.println(" "+this.subject_code[i]+" "+this.sub[i]+" "+this.subject_credit[i]+"
"+this.grade[i]);
}
System.out.println("\nSPI is"+this.spi);
System.out.println("");
}
public static void main(String str[])
{
Scanner sca=new Scanner(System.in);
int n,i;
System.out.print("Enter the how many Student? ");
n=sca.nextInt();
Studentspi st[]=new Studentspi[n];
for(i=0;i<n;i++)
{
st[i]=new Studentspi();
}
for(i=0;i<n;i++)
{
st[i].calculate_spi();
}
}
}
Output
3. Define the Rectangle class that contains: Two double fields x and y that specify the center of the rectangle, the data field width and height , A no-arg constructor that creates the default rectangle with (0,0) for (x,y) and 1 for both width and height.
- A parameterized constructor creates a rectangle with the specified x, y, height and width.
- A method getArea() that returns the area of the rectangle.
- A method getPerimeter() that returns the perimeter of the rectangle.
- A method contains(double x, double y) that returns true if the specified point(x,y) is inside this rectangle.
Write a test program that creates two rectangle objects. One with default valuesand other with user specified values. Test all the methods of the class for boththe objects.
class Rectangle
{
double x,y,width,height;
Rectangle()
{
x=0;
y=0;
width=1;
height=1;
}
Rectangle(double x,double y,double w,double h)
{
this.x=x;
this.y=y;
this.width=w;
this.height=h;
}
double getArea()
{
return (this.width*this.height);
}
double getperimeter()
{
return (2*(this.width+this.height));
}
boolean contains(double p,double q)
{
double x1,x2,x3,x4,y1,y2,y3,y4;
x1=x4=(this.x-(this.width/2));
x2=x3=(this.x+(this.width/2));
y1=y2=(this.y-(this.height/2));
y3=y4=(this.y+(this.height/2));
if((p>=x1 && p<=x2) && (q>=y1 && q<=y3))
{
return true;
}
else
{
return false;
}
}
}
class RectangleMain
{
public static void main(String a[])
{
Rectangle ract1=new Rectangle();
System.out.println("Area of Rectangle is: "+ract1.getArea());
System.out.println("Perimeter of Rectangle is: "+ract1.getperimeter());
System.out.println("(x,y) Cordinates is Inside in Rectangle? \n Ans: "+ract1.contains(10,10));
Rectangle ract2=new Rectangle(50,50,30,20);
System.out.println("Area of Rectangle is: "+ract2.getArea());
System.out.println("Perimeter of Rectangle is: "+ract2.getperimeter());
System.out.println("(x,y) Cordinates is Inside in Rectangle? \n Ans: "+ract2.contains(40,90));
}
}
Output
4. Create a new class called “City” that can be used to keep track of location information for a given city. Your class should include the following – be sure to comment your class appropriately:
a. String name
b. double lon (for longitude)
c. double lat (for latitude)
d. A constructor that accepts a name, lon and lat value and stores them in the instance variables for the object
e. A method that reports the current position of a city. Here is a method header to get
you started:
public void report()
f. A method that computes the distance from the lon and lat of one city to the lon
and lat of another city. Use the standard distance formula to compute this value (let’s
pretend that the cities lie on a flat plane and not on a sphere!) Here’s a method header
to get you started:
public double distanceFrom(City otherCity)
Create a new class called “Program4”. Do the following in this class:
a. Create an instance of City with name=Rajkot, lon=70.802160, lat=22.303894.
b. Create another instance of City with name=Surat, lon=72.831061, lat=21.170240.
c. Create another instance of City with name=Mumbai, lon=72.877656, lat=19.075984.
class City
{
String name;
double lon,lat;
City(String name,double lo,double la)
{
this.name=name;
this.lon=lo;
this.lat=la;
}
void report()
{
System.out.println("City: "+this.name);
System.out.println("Longitude: "+this.lon);
System.out.println("Latitude: "+this.lat);
}
double distanceFrom(City othercity)
{
double R=6371;
double dlon = deg2rad(othercity.lat - this.lat);
double dlat = deg2rad(othercity.lon - this.lon);
double a = Math.pow((Math.sin(dlat/2)),2) + Math.cos(deg2rad(this.lat)) * Math.cos(deg2rad(othercity.lat)) *
Math.pow((Math.sin(dlon/2)),2);
double c = 2 * Math.atan2( Math.sqrt(a), Math.sqrt(1-a) );
double d = R * c;// (where R is the radius of the Earth)
return d;
}
double deg2rad(double d)
{
return d * (Math.PI/180);
}
}
class CityDemo
{
public static void main(String a[])
{
City c1=new City("Rajkot",70.8021599,22.3038945);
c1.report();
City c2=new City("Surat",72.8310607,21.1702401);
c2.report();
System.out.println("Distance Between "+c1.name+" and "+c2.name+" "+c1.distanceFrom(c2)+" KM");
City c3=new City("Mumbai",72.877656,19.075984);
c2.report();
System.out.println("Distance Between "+c1.name+" and "+c3.name+" "+c1.distanceFrom(c3)+" KM");
}
}
Output
5. Design a class named Fan to represent a fan. The class contains:
- Three constants named SLOW, MEDIUM and FAST with values 1,2 and 3 to denote
the fan speed.
- An int data field named speed that specifies the speed of the fan (default SLOW).
- A boolean data field named f_on that specifies whether the fan is on(default false).
- A double data field named radius that specifies the radius of the fan (default 4).
- A data field named color that specifies the color of the fan (default blue).
- A no-arg constructor that creates a default fan.
- A parameterized constructor initializes the fan objects to given values.
- A method named display() will display description for the fan. If the fan is on, the
display() method displays speed, color and radius. If the fan is not on, the method returns
fan color and radius along with the message “fan is off.
- Write a test program that creates two Fan objects. One with default values and the other
with medium speed, radius 6, color brown, and turned on status true.
- Display the descriptions for two created Fan objects.
class Fan
{
final int slow=1,medium=2,fast=3;
int speed;
boolean f_on;
double radius;
String color;
Fan()
{
speed=slow;
f_on=false;
radius=4;
color="blue";
}
Fan(int sp,boolean fon,double r,String co)
{
speed=sp;
f_on=fon;
radius=r;
color=co;
}
void display()
{
if(this.f_on==false)
{
System.out.println("\nFan is 'OFF' ");
System.out.println("Fan Color: "+this.color);
System.out.println("Fan Radius: "+this.radius);
System.out.println("");
}
else
{
System.out.println("Fan is 'ON'");
System.out.println("Fan Speed: "+this.speed);
System.out.println("Fan Color: "+this.color);
System.out.println("Fan Radius: "+this.radius);
}
}
}
class FanDemo
{
public static void main(String args[])
{
Fan f1=new Fan();
Fan f2=new Fan(f1.medium,true,6,"brown");
f1.display();
f2.display();
}
}
Output
0 comments:
Post a Comment