Вы находитесь на странице: 1из 25

1.

Design and Implement the Student Class

Write program to have the Student class in Student.java and a tester program at StudentTester.java.

Write a constructor of Student class to accept First Name, Last Name, Grade (Class in which the
student enrols like 1, 2, 3 etc)

Create two instances of Student Class in the Tester Program with First Name, Last Name and Grade.
Make a report displaying the rank (decending) along with the names (First Name & Last Name
Together)

Use Collection

Program:

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Scanner;

class Studenttest

String firstname,secondname;

int grade,rank;

Studenttest(String firstname,String secondname,int grade) // parameterized constructor

Scanner inp=new Scanner(System.in);

this.firstname=firstname;

this.secondname=secondname;

this.grade=grade;

System.out.println("Enter the rank :");

rank=inp.nextInt();

public String toString() // toString to print the value


{

return("firstname : "+firstname +

"secondname : "+secondname+

"grade : "+grade +

"rank :"+rank );

public static Comparator<Studenttest> compno=new Comparator<Studenttest>() //compares


the rank value

public int compare(Studenttest s1,Studenttest s2)

return(s2.rank-s1.rank);

};

public class Student

public static void main(String args[])

List<Studenttest> li=new ArrayList<Studenttest>();

Studenttest obj1=new Studenttest("veena","b",3);

Studenttest obj2=new Studenttest("sita","s",1);

li.add(obj1);

li.add(obj2);

System.out.println(li);

Collections.sort(li,Studenttest.compno); // sorts in descending order


System.out.println("descending order");

System.out.println(li);

Output:

2. Create an Abstract Class Shape (to define Geometrical Shapes) Add abstract methods –
getPerimeter, getArea to abstract class.

Create Square, Pentagon, Circle

Program:

abstract class Shape1

abstract void getPerimeter();

abstract void area();

class Square extends Shape1


{

float side=10;

public void getPerimeter()

float sp=4*side;

System.out.println("Perimeter of square : "+sp);

public void area()

float area=side*side;

System.out.println("Area of square : "+area);

class Pentagon extends Shape1

float side=6.4f;

public void getPerimeter()

float pent=5*side;

System.out.println("perimeter of pentagon :" +pent);

public void area()

float area=5.295f*side*side;

System.out.println("Area od pentagon :"+area);

class Circle extends Shape1


{

float radius=7;

public void getPerimeter()

float cir=2*3.14f*radius;

System.out.println("perimeter of circle :" +cir);

public void area()

float area=3.14f*radius;

System.out.println("Area of circle : "+area);

public class Shape

public static void main(String args[])

Square sobj=new Square();

sobj.getPerimeter();

sobj.area();

Pentagon pobj=new Pentagon();

pobj.getPerimeter();

pobj.area();

Circle cobj=new Circle();

cobj.getPerimeter();

cobj.area();

}}
Output:

3. Implement a sequential search function that takes an ArrayList<Integer> as a parameter

Program:

import java.util.ArrayList;

import java.util.Scanner;

public class Sequential

public void search(ArrayList al,int value)

int flag=0 ,count=0;

for(int i=0;i<al.size();i++)

if(value==(int)al.get(i))

flag=1;

if(flag==1)

{
System.out.println("found");

else

System.out.println("not found");

public static void main(String[] args)

Scanner inp=new Scanner(System.in);

ArrayList<Integer> al=new ArrayList<Integer>();

Sequential obj=new Sequential();

al.add(12);

al.add(20);

al.add(4);

al.add(7);

al.add(17);

System.out.println(al);

System.out.println("Enter the value to be searched ");

int value=inp.nextInt();

obj.search(al,value);

}
Output:

4. Create a class fior Employee and store Employee ID for 20 Employee and sort in desecnding order.

Program:

Class Name:- Employee

Method Name:- Input( For Input Employee ID)

Method Name:- Display for Show details for Employee.

Program:

import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

import java.util.Scanner;

class Employee

int empid;

String name;

public void input()

Scanner inp=new Scanner(System.in);

System.out.println("Enter the empid");

empid=inp.nextInt();

inp.nextLine();
System.out.println("Enter the empname :");

name=inp.nextLine();

public String toString()

return ("employee id : "+empid +

"employee name : "+name);

public static Comparator<Employee> eid=new Comparator<Employee>()

public int compare(Employee e1,Employee e2)

return(e2.empid-e1.empid);

};

public class EmployeeDetails {

public static void main(String[] args)

List<Employee> li=new ArrayList<Employee>();

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

Employee obj=new Employee();

obj.input();

li.add(obj);

}
Collections.sort(li,Employee.eid);

System.out.println(li);

Output:

5. Modify the sequential search and binary search functions to return the number of comparisons
made in each search, which one is more efficient? Test the functions on arrays

Program:

import java.util.ArrayList;

import java.util.Scanner;

public class Binaryseq

public static void sequential(int arr[],int value)

{
int count=0;

for(int i=0;i<arr.length;i++)

if(arr[i]==value)

System.out.println("element found at "+i);

count++;

System.out.println("sequential search count :"+count);

public static void binary(int arr[], int first, int last, int value)

int mid = (first + last)/2;

int count=0;

while( first <= last )

if ( arr[mid] < value)

first = mid + 1;

else if ( arr[mid] == value)

System.out.println("Element is found at index: " + mid);

break;

else
{

last = mid - 1;

mid = (first + last)/2;

count++;

System.out.println("binary search count :"+count);

/* if ( first > last ){

System.out.println("Element is not found!");

} */

public static void main(String[] args)

Scanner inp=new Scanner(System.in);

Binaryseq obj=new Binaryseq();

int arr[]=new int[6];

int i;

System.out.println("Enter the array elements ");

for(i=0;i<6;i++)

arr[i]=inp.nextInt();

int last=arr.length-1;

System.out.println("Enter the value to be searched ");

int value=inp.nextInt();

sequential(arr,value);
binary(arr,0,last,value);

System.out.println("binary search is efficient");

Output:

6.. Create an array for size of 10 and insert Car Number and create two another array and put even
and odd car number in repesctive different arrays without any index gapping

Program.

import java.util.Scanner;

public class Carnumber {

public static void main(String[] args)

Scanner inp=new Scanner(System.in);

int car[],odd[],even[],i;

car=new int[10];
even=new int[10];

odd=new int[10];

System.out.println("Enter the car number ");

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

car[i]=inp.nextInt();

System.out.println("even car number ");

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

if(car[i]%2==0)

even[i]=car[i];

System.out.println(even[i]);

System.out.println("odd car number ");

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

if(car[i]%2 !=0)

odd[i]=car[i];

System.out.println(+odd[i]);

}
Output:

7. . Write a program for matrix multiplication.

Use 3X3 matrix

Program:

import java.util.Scanner;

public class Matrixmul

public static void main(String[] args)

Scanner inp=new Scanner(System.in);

int mat1[][]={{3,4,5},{7,8,2},{3,5,6}};

int mat2[][]={{5,6,3},{1,2,3},{5,6,2}};

int mat3[][]=new int[3][3];


int i,j,k;

System.out.println("Multiplication matrix");

for(i=0;i<3;i++)

for(j=0;j<3;j++)

mat3[i][j]=0;

for(k=0;k<3;k++)

mat3[i][j]+=mat1[i][k]*mat2[k][j];

System.out.print(mat3[i][j] +" ");

System.out.println(" ");

Output:
8. public class Type

double methodA(byte x, double y)

return x / y * 2;

public static void main(String[] args) {

byte x=1;

double y=2;

Type pobj = new Type();

System.out.println("result = "+pobj.methodA(x,y));

Output:

9. Write an ExpandingArray class that stores an array as an instance variable and supports the
methods

• public void add(int index, int element)

• public void add(int element)

• public int remove(int index)

• public int size()

• public String toString()

Program:
import java.util.ArrayList;

import java.util.Scanner;

public class Expandingarray

ArrayList<Integer> li=new ArrayList<Integer>();

public void add(int index, int element)

li.add(index, element);

public void add(int element)

li.add(element);

public int remove(int index)

return li.remove(index);

public int size()

for(int j:li)

System.out.println(j);

return li.size();
}

public String toString()

return null;

public static void main(String[] args)

int index,element,choice;

String value;

Scanner inp=new Scanner(System.in);

Expandingarray obj=new Expandingarray();

do

System.out.println("Enter the choice ");

System.out.println("1.add element in index");

System.out.println("2.add");

System.out.println("3.remove");

System.out.println("4.size");

System.out.println("5.exit ");

choice=inp.nextInt();

switch(choice)

case 1:

System.out.println("Enter the index");

index=inp.nextInt();
System.out.println("Enter the element");

element=inp.nextInt();

obj.add(index,element);

break;

case 2:

System.out.println("Enter the element");

element=inp.nextInt();

obj.add(element);

break;

case 3:

System.out.println("Enter the index");

index=inp.nextInt();

obj.remove(index);

break;

case 4:

System.out.println("display the size");

obj.size();

break;

case 5:

System.out.println("exit");

break;

inp.nextLine();

System.out.println("Do u want to continue :");

value=inp.nextLine();

}while(value.equals("yes"));

}
}

Output:

10.

The following code prepare for addition of two numbers.

int a, b;

int sum=a+b;

System.out.println(“Enter two numbers to add: ");


System.out.println (a);

System.out.println (b);

System.out.println (“The sum is: “+sum);

1. It is valid declaration.

2. What is output of this program

Output: variable a and b has no initialization. Compilation error.

11.

The following program passes this character to println. What does it print?

public class LinePrinter

public static void main(String[] args)

char c = 0x000A; System.out.println(c); } }

1. Does the syntax work?

2. What does it print?

3. Is its behavior platform dependent?

Output:

The value for 0x000A is 10. It is platform independent behavior. It cannot complie on any platform.
12.

This one contains a loop that keeps track of how many iterations it takes to
terminate. Unlike that program, this one uses the leftshift operator (<<)

public class Shifty {

public static void main(String[] args) {

int i = 0;

while (-1 << i != 0)

i++; System.out.println(i);

}}

1. What is output

Output: no output

13.

This program adds an unusual twist to the usual Hello world program. What
does it print?

try

{ System.out.println("Hello world");

System.exit(0);

Finally

{ System.out.println("Goodbye world"); }

1. Does the syntax work?


2. If syntax is not right so what we will remove in this syntax

Output:

Syntaz error.

Error in finally. f should he small.

14.

class San

San()throws IOException

class Foundry extends San

Foundry()

public static void main(String[] args)

1. Does the syntax work


2. If syntax is correct so what is output.

3. If syntax is not correct so which exception occurred

4. Then fix the Problem

Output:

15.

This program will work

static final int x = 11;

private int y = 33;

public void method1(int x)

Test t = new Test();

x = 22;

y = 44;

System.out.println("Test.x: " + Test.x);

System.out.println("t.x: " + t.x);

System.out.println("t.y: " + t.y);

System.out.println("y: " + y);

1) What does program print?

2) If any error then fix it.

Вам также может понравиться