Lab Task

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

LabTask-1

Name: K.Rakesh
Regn.no: 17BIS0069
Course Code: ITE2005
01. Define a class called as StudentBase containing necessary details about the name, date of birth,
blood group and nationality. Define another class called Academics inherited from Student to store
the number of courses registered in the patrticluar semester and grade scored in each semester.
Define a driver class used to capture the details of aleast 5 students and display the result of those
students whose credits are greater than 25.

Code:

package javaa;

class Student{
String name;
String dob;
String nation;
String bgroup;
}
class Acadamics extends Student{
int no_of_courses;
float[] grade;
Acadamics(String s1,String s2,String s3,String s4,int
num, float[] grades){
name=s1;
dob=s2;
nation=s3;
bgroup=s4;
no_of_courses = num;
grade=grades;
}

public class first {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Acadamics a[]=new Acadamics[5];
a[0]= new Acadamics("rakesh","15-jan-
2000","India","B+",6,new float[]{8.7f,9.27f,8.95f});
a[1]= new Acadamics("yaswanth","15-aug-
2000","India","AB-",4,new float[]{5.5f,5.56f,7.21f});
a[2]= new Acadamics("dileep","15-april-
2000","India","B-",2,new float[]{4.7f,2.63f,7.88f});
a[3]= new Acadamics("venkat","15-dec-
2000","India","O+",6,new float[]{9.3f,9.57f,7.56f});
a[4]= new Acadamics("sowrav","15-Sept-
2000","India","B+",6,new float[]{1.7f,6.33f,7.79f});
for(int i=0;i<5;++i){
if(a[i].no_of_courses>5)
System.out.println("Student name:
"+a[i].name);
}
}

}
Result:
02. Define a Abstract class called Shape and derive the shapes like Rectangle, Square, Triangle
and Circle from it. Provide appropriate definition for computing area of the shapes. Apply
runtime polymorphism.
Code:

package javaa;
abstract class Shape{
abstract float calculateArea();
}
class Rectangle extends Shape{
public float len,bred;
public float calculateArea() {
return len*bred;
}
}
class Square extends Shape{
public float side;
public float calculateArea() {
return side*side;
}
}
class Triangle extends Shape{
public float base,hei;
public float calculateArea() {
return (0.5f*base*hei);
}
}
class Circle extends Shape{
public float r;
public float calculateArea() {
return 3.14f*r*r;
}
}
public class Test3 {

public static void main(String[] args) {


Rectangle r=new Rectangle();
r.len=1;
r.bred=3;
Square s= new Square();
s.side=7;
Triangle t = new Triangle();
t.base=2;
t.hei=5;
Circle c = new Circle();
c.r =1;
System.out.println("Rectangle Area:
"+r.calculateArea());
System.out.println("Square: "+s.calculateArea());
System.out.println("Triangle: "+t.calculateArea());
System.out.println("Circle "+c.calculateArea());
}}

Output:
3. Define an interface Sports containing the data member for holding the weightage for the sports
activity (say value as 8) and the method named calculateScore(). Deine a class called Personal
and Academic to store the relevant details about the student. Implmentation of the method
calculateScore() would add the wightage based on the score obtained the aacademic courses.
For example, in case of 5 subjects if the score is between 450-500 then sports weightage would
be the (weightage constant X score/100) and add the new score to the score obtained.
Code:

package javaa;
import java.util.*;
interface i{
float weightage=8;
float calculateScore();
}
class PersonalandAcadamic implements i{

PersonalandAcadamic(int m1,int m2,int m3,int m4,int m5){


score[0]=m1;
score[1]=m2;
score[2]=m3;
score[3]=m4;
score[4]=m5;
}
int[] score =new int[5];
public float calculateScore(){
int result=0;
for(int i=0;i<5;++i){
result=result+score[i];
}
return (weightage*result)/100+result;
}
}
public class Test4 {
/**
* @param args
*/
public static void main(String[] args)throws Exception {
PersonalandAcadamic obj =new
PersonalandAcadamic(90,80,80,96,75);
System.out.println(obj.calculateScore());
}

Output:

04. Develop a java program to convert the input string into the format given below:
“I love Java” as “aIa aevola aavaJa”
Code:

package javaa;
public class Test1 {

public static void main(String[] args) {


// TODO Auto-generated method stub
String s="I Love Java";
String[] sarray = s.split(" ");
StringBuffer res= new StringBuffer();
for(String str:sarray)
res.append(new StringBuffer("
a"+str+"a").reverse());

System.out.println(res);
}
}

Output:
05. Given the three sides of the triangle a,b and c, calculate the area of the triangle using the
formula area = √s(s-a)(s-b)(s-c), where s = (a+b+c)/2. Throw an user defines exeption with
appropriate message if sume of two sides exceds the other.

Code:

package javaa;

import java.util.*;

import java.lang.*;

import java.io.*;

class MyException extends Exception {

String str;

MyException(String s){

str = s;

public String toString() {

return str;

public class Test {

public static void main (String[] args)throws MyException {

//code

Scanner sc = new Scanner(System.in);

int a= sc.nextInt();

int b = sc.nextInt();

int c = sc.nextInt();
if(a>b+c)

throw new MyException("A is greater that sum of other two sides");

if(b>a+c)

throw new MyException("B is greater that sum of other two sides");

if(c>a+b)

throw new MyException("C is greater than Sum of other two Sides");

double s=(a+b+c)*0.5;

double ans=Math.sqrt(s*(s-a)*(s-b)*(s-c));

System.out.println(ans);

OUTPUT:

You might also like