Answers Java Practical
Note:- Guys if any program of code not show proper code then please all code copy and paste in our system editor such as notepad, vsc code or another editor.
Q1. Write a program to create an applet displaying Hello World.
Program :-
Java File Name :- Simple.java
import java.applet.*;
import java.awt.*;
public class Simple extends Applet{
public void paint(Graphics g){
g.drwaString("Hello World", 300, 200);
}
}
// If your java version = 1.8, I mean very old version, then run otherwise not run.
HTML File Name: Demo.html
<html>
<body>
<applet code="Simple.class", width="200" height="200">
</applet>
</body>
</html>
*************Output***************
Notes : Guy's Output Is show on your browsers, but of you browser is supported applet program then show otherwise not show,
Q2. Write a program to take three numbers from command line and find the largest among them.
Program
class Largest{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int a,b,c;
System.out.println("Enter Three Number Continue");
System.out.print("Enter 1st Number:");
a=sc.nextInt();
System.out.print("Enter 2nd Number:");
b=sc.nextInt();
System.out.print("Enter 3rd Number:");
c=sc.nextInt();
if(a>b){
if(a>c){
System.out.println("1st Number is greatest");
}
else{
System.out.println("3rd Number is greatest");
}
}
else{
if(b>c){
System.out.println("2nd Number is greatest");
}
else{
System.out.println("3rd Number is greatest");
}
}
}
}
*************Output***************
1st Run:
Enter Three Number Continue
Enter 1st Number:4
Enter 2nd Number:5
Enter 3rd Number:6
3rd Number is greatest
2nd Run:
Enter Three Number Continue
Enter 1st Number:9
Enter 2nd Number:6
Enter 3rd Number:3
1st Number is greatest
Q3. Write a program to print the table of given number from command line in alternate way as follows.
Ex.
Suppose given number is 2 Suppose given number is 5
Output 2,6,10,14,18 Output 5,15,25,35,45
Program
EXample -1:
// EXample 1:- Suppose given number is 2
class Table2{
public static void main(String args[]){
int t=2;
Scanner sc= new Scanner (System.in);
System.out.print("Enter Last Number:");
int num= sc.nextInt();
for(int i=2; i<num; i++){
System.out.print(t+ " ");
t=t+4;
}
}
}
*************Output***************
Enter Last Number:10
2 6 10 14 18 22 26 30
EXample -2:
// EXample 2:- Suppose given number is 5
class Table5{
public static void main(String args[]){
int t=5;
Scanner sc= new Scanner (System.in);
System.out.print("Enter Last Digit:");
int num= sc.nextInt();
for(int i=2; i<num; i++){
System.out.print(t+" ");
t=t+10;
}
}
}
*************Output***************
Enter Last Digit:10
5 15 25 35 45 55 65 75
Q4. Create a Simple program using class for students having roll number, mobile number with name as data.
Program
class Student{
String name;
int roll, marks;
double mob;
Scanner sc=new Scanner(System.in);
public void getdata(){
System.out.print("Enter Student Name: ");
name = sc.nextLine();
System.out.print("Enter Student Roll Number:");
roll =sc.nextInt();
System.out.print("Enter Student Mobile Number:");
mob= sc.nextDouble();
System.out.print("Enter Student Marks:");
marks= sc.nextInt();
}
public void showdata(){
System.out.println("Student Name is :"+name);
System.out.println("Student Roll Number is :"+roll);
System.out.println("Student Mobile is :"+mob);
System.out.println("Student Marks is :"+marks);
}
public static void main(String arg[]){
Student obj= new Student();
obj.getdata();
System.out.println("\n******************************************************");
System.out.println("****Student Data has been Successfully Saved *********");
System.out.print("****************************************************** \n");
obj.showdata();
}
}
*************Output***************
Enter Student Name: Shailesh
Enter Student Roll Number:82000
Enter Student Mobile Number:8210600000
Enter Student Marks:456
******************************************************
****Student Data has been Successfully Saved *********
******************************************************
Student Name is :Shailesh
Student Roll Number is :82000
Student Mobile is :8.2106E9
Student Marks is :456
Q5. WAP to show the creation and use of a thread.
Program
class MyThread extends Thread{
public void run(){
int a=10, b=20;
int c=a+b;
System.out.println("Addition of a and b: "+c);
}
}
class DemoThread{
public static void main(String args[]){
MyThread obj= new MyThread();
obj.start();
}
}
*************Output***************
Addition of a and b: 30
Q6. WAP to show the use of “for - each” loop.
Program
//for-each loop:- Special type of loop work with array
class Loop{
public static void main(String arg[]){
int arr[]={10,30,40,50,54,34};
// String arr[]={"Shailesh","Raj", "Annu","Rahul", "Ayush"};
for(int element:arr){
System.out.print(element+" ");
}
}
}
*************Output***************
10 30 40 50 54 34
//Same Program run For Loop.
class Loop{
public static void main(String arg[]){
int arr[]={10,30,40,50,54,34};
for(int i=0; i<arr.length; i++){
System.out.print(arr[i]+" ");
}
}
}
*************Output***************
10 30 40 50 54 34
Q7. WAP to show the use of method overloading .
Program
// method overloading:- In this case same name as function but different-different parameter
class BaiscMath{
void area(int height, int width){
int rect=height*width;
System.out.println("Area of Rectangle:-"+rect);
}
void area(double raduis){
double cir=22/7*raduis*raduis;
System.out.println("Area of Circle:-"+cir);
}
void area(float side ){
float cube= side*side*side;
System.out.println("Area of Cube :-"+cube);
}
}
class Allarea{
public static void main(String arg[]){
BaiscMath obj= new BaiscMath();
obj.area(4,5);
obj.area(7.56);
obj.area(5);
}
}
*************Output***************
Area of Rectangle:-20
Area of Circle:-171.46079999999998
Area of Circle:-75.0
Q8. WAP to show the use of method overriding.
Hints:- method overridig:- It means that Same function Name And Same parameter Value but create two class, because same fucntion name and same parameter can't access in one class.
Program
class Rectan1{
void area(int height, int width){
int rect= height*width;
System.out.println("Area of Rectangle 1st Class:"+rect);
}
}
class Rectan2 extends Rectan1{
void area(int height, int width){
int rect= height*width;
System.out.println("Area of Rectangle 2nd Class:"+rect);
}
public static void main(String arg[]){
Rectan2 obj2= new Rectan2();
obj2.area(5,7);
Rectan1 obj1= new Rectan1();
obj1.area(2,4);
}
}
*************Output***************
Area of Rectangle 2nd Class:35
Area of Rectangle 1st Class:8
Q9. Write a program show simple inheritance in java.
Hints:- inheritance :- It means that getting properities of parent class into the Child Class.
Inn another Words: Derived all data and members function of Base class
There are Five types Three Types in Java:- Single, Multilevel, And Hybrid
Example Of Single inheritance:- In this Case Derived Class access all properities of Base class
Program
class Base{
public void add(int a, int b){
int c= a+b;
System.out.println("Addition of a and b(Base Class):"+c);
}
}
class Derived extends Base{
void sub(int a, int b){
int c= a-b;
System.out.println("Substraction of a and b(Derived Class):"+c);
}
}
class Single{
public static void main(String arg[]){
Derived obj= new Derived();
obj.add(7,9);
obj.sub(10,5);
}
}
*************Output***************
Addition of a and b(Base Class):16
Substraction of a and b(Derived Class):5
Q10. Write a program to show the use of super in Java.
Hints:- Super :- super is keyword that is refers that the object of current base class, It is used for replace derived class variable with base class, it is also called base class varaible , methods and base class constructor.
Program
class Super{
int height=10, width=20;
}
class Sub extends Super{
int height=5, width=6;
public void area(){
int ar= height*width;
System.out.println("Area of Rectangle(without super class):"+ar);
ar=super.height* super.width;
System.out.println("Area of Rectangle (With Super class):"+ar);
}
}
class SuperVar{
public static void main(String args[]){
Sub obj= new Sub();
obj.area();
}
}
*************Output***************
Area of Rectangle(without super class):30
Area of Rectangle (With Super class):200
Guy's Finally Done This Practical all Questions. I hope this post is helpful for you. By The way Don't forget follow me.
Comments