Академический Документы
Профессиональный Документы
Культура Документы
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;
this.firstname=firstname;
this.secondname=secondname;
this.grade=grade;
rank=inp.nextInt();
return("firstname : "+firstname +
"secondname : "+secondname+
"grade : "+grade +
"rank :"+rank );
return(s2.rank-s1.rank);
};
li.add(obj1);
li.add(obj2);
System.out.println(li);
System.out.println(li);
Output:
2. Create an Abstract Class Shape (to define Geometrical Shapes) Add abstract methods –
getPerimeter, getArea to abstract class.
Program:
float side=10;
float sp=4*side;
float area=side*side;
float side=6.4f;
float pent=5*side;
float area=5.295f*side*side;
float radius=7;
float cir=2*3.14f*radius;
float area=3.14f*radius;
sobj.getPerimeter();
sobj.area();
pobj.getPerimeter();
pobj.area();
cobj.getPerimeter();
cobj.area();
}}
Output:
Program:
import java.util.ArrayList;
import java.util.Scanner;
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");
al.add(12);
al.add(20);
al.add(4);
al.add(7);
al.add(17);
System.out.println(al);
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:
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;
empid=inp.nextInt();
inp.nextLine();
System.out.println("Enter the empname :");
name=inp.nextLine();
return(e2.empid-e1.empid);
};
for(int i=0;i<20;i++)
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;
{
int count=0;
for(int i=0;i<arr.length;i++)
if(arr[i]==value)
count++;
public static void binary(int arr[], int first, int last, int value)
int count=0;
first = mid + 1;
break;
else
{
last = mid - 1;
count++;
} */
int i;
for(i=0;i<6;i++)
arr[i]=inp.nextInt();
int last=arr.length-1;
int value=inp.nextInt();
sequential(arr,value);
binary(arr,0,last,value);
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;
int car[],odd[],even[],i;
car=new int[10];
even=new int[10];
odd=new int[10];
for(i=0;i<10;i++)
car[i]=inp.nextInt();
for(i=0;i<10;i++)
if(car[i]%2==0)
even[i]=car[i];
System.out.println(even[i]);
for(i=0;i<10;i++)
if(car[i]%2 !=0)
odd[i]=car[i];
System.out.println(+odd[i]);
}
Output:
Program:
import java.util.Scanner;
int mat1[][]={{3,4,5},{7,8,2},{3,5,6}};
int mat2[][]={{5,6,3},{1,2,3},{5,6,2}};
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.println(" ");
Output:
8. public class Type
return x / y * 2;
byte x=1;
double y=2;
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
Program:
import java.util.ArrayList;
import java.util.Scanner;
li.add(index, element);
li.add(element);
return li.remove(index);
for(int j:li)
System.out.println(j);
return li.size();
}
return null;
int index,element,choice;
String value;
do
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:
index=inp.nextInt();
System.out.println("Enter the element");
element=inp.nextInt();
obj.add(index,element);
break;
case 2:
element=inp.nextInt();
obj.add(element);
break;
case 3:
index=inp.nextInt();
obj.remove(index);
break;
case 4:
obj.size();
break;
case 5:
System.out.println("exit");
break;
inp.nextLine();
value=inp.nextLine();
}while(value.equals("yes"));
}
}
Output:
10.
int a, b;
int sum=a+b;
System.out.println (b);
1. It is valid declaration.
11.
The following program passes this character to println. What does it print?
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 (<<)
int 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"); }
Output:
Syntaz error.
14.
class San
San()throws IOException
Foundry()
Output:
15.
x = 22;
y = 44;