Java All Ans
Java All Ans
Solution: class
PrNo
{
public static void main (String[] args)
{
int size = args.length;
int[] array = new int [size];
for(int i=0; i<size; i++)
Slip 1_2: Define an abstract class Staff with protected members id and name. Define a parameterized
constructor. Define one subclass OfficeStaff with member department. Create n objects of OfficeStaff
and display all details.
Solution:
import java.util.*;
abstract class Staff
{
protected int id;
protected String name;
public Staff(int id,String name)
{
this.id=id;
this.name=name;
}
}
class OfficeStaff extends Staff
{
String dept;
OfficeStaff(int id,String name,String dept)
{
for(int i=0;i<n;i++)
{
ob[i].display();
}
}
}
Slip2_1: Write a program to read the First Name and Last Name of a person, his weight
and height using command line arguments. Calculate the BMI Index which is defined
as the individual's body mass divided by the square of their height.
(Hint : BMI = Wts. In kgs / (ht)2
// body mass index class
BM {
public static void main(String args[]) {
String fname = args[0];
String lname = args[1];
double weight = Double.parseDouble(args[2]);
temp2=c[i].inning;
c[i].inning=c[j].inning;
c[j].inning=temp2;
temp3=c[i].tofnotout;
c[i].tofnotout=c[j].tofnotout;
c[j].tofnotout=temp3;
temp4=c[i].totalruns;
c[i].totalruns=c[j].totalruns;
c[j].totalruns=temp4;
System.out.println("Enter the limit:");
}
Cricket.avg(n,c);
Cricket.sort(n, c);
for(int i=0;i<n;i++){
c[i].put();
}
Slip3_1: Write a program to accept ‘n’ name of cities from the user and sort them in
ascending order.
import java.util.Scanner;
class SortStr
{
public static void main(String args[])
{
String name;
this.age=age;
String name;
}
}
double level,hrct;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter name: "); name=br.readLine();
System.out.println("Enter the age: ");
age=Integer.parseInt(br.readLine()); System.out.println("Oxygen
level: "); level=Double.parseDouble(br.readLine());
System.out.println("HRCT report: ");
hrct=Double.parseDouble(br.readLine()); Patient ob=new
Patient(name,age,level,hrct); try{
if(ob.level<95 && ob.hrct>10)
else
System.out.println("Patient Info: \n"+"Name: "+ob.name+"\nAge: "+ob.age+"\nHRCT
report: "+ob.hrct+"\nOxygen level:"
dimensional array.
{
for(int j=0;j<c;j++)
{
mat[i][j] =
sc.nextInt();
}
}
System.out.println("the matrix is:"); for(int
i=0;i<c;i++)
{
for(int
j=0;j<r;j++)
{
System.out.print(" " +mat[j][i]);
}
System.out.println(" ");
}
User can have 3 login chances only. Use clear button to clear the TextFields.
char c= '*' ;
Label uname,upass;
Panel p; int
attempt=0;
{
p=new Panel();
uname=new Label("Use Name: "
,Label.CENTER); upass=new Label ("Password:
",Label.RIGHT); nametext=new TextField(20);
passtext =new TextField(20);
passtext.setEchoChar(c); msg=new TextField(10);
msg.setEditable(false); login=new
Button("Login");
Clear=new Button("Clear");
login.addActionListener(this);
Clear.addActionListener(this); p.add(uname);
p.add(nametext);
p.add(upass);
p.add(passtext);
p.add(login);
p.add(Clear);
p.add(msg); add(p);
Button btn=(Button)(ae.getSource());
passtext.setText("");
setTitle("Login ");
setSize(290,200);
setResizable(false);
setVisible(true);
}
String user=nametext.getText();
{
msg.setText("Valid");
System.out.println("Username is valid");
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e)
{
msg.setText("Error");
}
attempt++;
}
}
else
{
System.out.println("you are using 3 attempt");
System.exit(0);
}
}
}
String sta;
void sta_input()throws IOException
{
System.out.println("Enter the state name:");
sta = r.readLine();} }
import java.util.*;
class Matrix
{
Scanner sc = new Scanner(System.in); int a =
sc.nextInt(); int b = sc.nextInt(); int M[][] = new
int[a][b];
void accept()
{
int a = this.a; int b = this.b;
System.out.println("enter the "+(a*b)+ " values to matrix:");
for(int i=0;i<a;i++)
{
for(int j=0;j<b;j++)
{
this.M[i][j] = sc.nextInt();
}
}
}
int choice;
Scanner scanner = new Scanner(System.in);
while(true) {
System.out.println("Press 1: Addition, 2: Multiplication, 3: Exit");
choice = scanner.nextInt();
switch (choice) { case 1:
System.out.println("Addition is:" );
for(int i=0;i<m1.a;i++)
{
for(int j=0;j<m1.b;j++)
{
System.out.print(" "+ (m1.M[i][j]+m2.M[i][j]));
}
System.out.println(" ");
}
break; case 2:
System.out.println("Multiplication is:"); for(int
i=0;i<m2.a;i++)
{
for(int j=0;j<m2.b;j++)
{
public String toString() // overrides toString() method
{
return id+" "+name+" "+salary+" "+desig;
}
public static void main(String args[])
{
Emp E1=new Emp(111,"Rakesh",50000,"bsc cs");
Emp E2=new Emp(112,"Suresh",25000,"msc cs");
System.out.println("Employee details: "+E1);
System.out.println("Employee details: "+E2);
}
}
Slip6_2: Create an abstract class “order” having members id, description. Create two
subclasses “PurchaseOrder” and “Sales Order” having members customer name and
Vendor name respectively. Definemethods accept and display in all cases. Create 3
objects each of Purchase Order and Sales Order and accept and display details.
import java.io.BufferedReader; import java.io.IOException; import
java.io.InputStreamReader; abstract class Order{
}
}
for(i=0;i<m;i++)
{
h[i].display();
System.out.println("Object is created:");
}
case 3:
System.out.println("exit:");
System.exit(0);
}
}
}
Slip7_1: Design a class for Bank. Bank Class should support following operations;
a. Deposit a certain amount into an account
b. Withdraw a certain amount from an account
c. Return a Balance value specifying the amount with details
class Bank {
private double balance;
balance = balance - amount;
}
Slip7_2: Write a program to accept a text file from user and display the contents of a file in
reverse order and change its case.
BufferedInputStream bis = new BufferedInputStream(new
System.out.print(ch);
bis.reset();
}
bis.close();
}
else
System.out.println("file not found");
}
}
Slip8_1: Create a class Sphere, to calculate the volume and surface area of sphere. (Hint : Surface
area=4*3.14(r*r), Volume=(4/3)3.14(r*r*r))
Slip8_2: Design a screen to handle the Mouse Events such as MOUSE_MOVED and
MOUSE_CLICKED and display the position of the Mouse_Click in a TextField.
import java.awt.*;
import java.awt.event.*;
class MyFrame extends Frame
{
TextField t,t1;
Label l,l1;
MyFrame(String title)
super(title);
setLayout(new FlowLayout());
p=new Panel();
p.setLayout(new GridLayout(2,2,5,5));
t=new TextField(20);
l= new Label("Co-ordinates of mouse clicking");
l1= new Label("Co-ordinates of mouse
movement"); t1=new TextField(20);
p.add(l);
p.add(t);
p.add(l1);
p.add(t1);
add(p);
addMouseListener(new MyClick());
addMouseMotionListener(new MyMove());
setSize(500,500);
setVisible(true);
}
class MyClick extends MouseAdapter
{
public void mouseClicked(MouseEvent me)
{
x=me.getX();
y=me.getY();
}
Slip9_1: Define a “Clock” class that does the following ;
a. Accept Hours, Minutes and Seconds
b. Check the validity of numbers
c. Set the time to AM/PM mode
Use the necessary constructors and methods to do the above task
import java.util.*; class
Clock
{
int hours,minutes,seconds;
Clock()
{
System.out.println("enter the time in HH MM SS format");
Scanner sc= new Scanner(System.in);
this.hours = sc.nextInt();
this.minutes = sc.nextInt();
this.seconds = sc.nextInt();
}
void isTimeValid()
{
if(hours>=0 && hours<24 && minutes>0 &&minutes<60
&&seconds>0 && seconds<60)
System.out.println("time is valid");
else
System.out.println("time is not valid");
}
}
count.
import java.util.*;
interface MarkerInt {
product() {
pid = 1; pcost =
10; quantity = 1;
pname = "pencil";
cnt++;
quantity = q;
}
Scanner sc = new Scanner(System.in);
sc.nextLine();
System.out.println("Enter Product Name: ");
String pn = sc.nextLine();
Slip10_1: Write a program to find the cube of given number using functional interface.
import java.util.*;
float cube();
}
class Draw implements Cube
{
public float cube()
{
System.out.println("enter the number");
Scanner sc= new Scanner (System.in); float
cu = sc.nextInt();
double cue = cu*cu*cu;
System.out.println("cube of no is:"+cue); return
0;
}
public static void main(String args[])
{
Draw d = new Draw();
d.cube();
}
}
System.out.println("Roll_no : "+r_no);
System.out.println("Name : "+name);
System.out.println("class :"+clas);
System.out.println("-----MARKS-------");
System.out.println("Sub 1 : "+a);
System.out.println("Sub 2 : "+b);
System.out.println("Sub 3 : "+c);
System.out.println("Sub 4 : "+d);
System.out.println("Sub 5 : "+e);
System.out.println("Sub 6 : "+f);
System.out.println("Total : "+sum);
System.out.println("percentage: "+per);
System.out.println("------------------");
}
}
int m1,m2,m3,m4,m5,m6;
System.out.print("Enter 6 sub mark:= ");
m1 = sc.nextInt();
m2 = sc.nextInt(); m3
= sc.nextInt();
m4 = sc.nextInt();
m5 = sc.nextInt();
m6 = sc.nextInt();
s.display();
}
}
Slip11_1: Define an interface “Operation” which has method volume( ).Define a constant PI
having a value 3.142 Create a class cylinder which implements this interface (members –
import java.io.*;
Slip11_2: Write a program to accept the username and password from user if username and password
are not same then raise "Invalid Password" with appropriate msg.
Userpassword()
}
if(user.compareTo(pass)==0)
System.exit(0);
}
else
{
throw new InvalidPasswordException();
}
}
catch(Exception e1)
{
cnt++;
JOptionPane.showMessageDialog(null, "Login Failed", "Login",
JOptionPane.ERROR_MESSAGE);
nameText.setText("");
passText.setText("");
nameText.requestFocus();
if(cnt==3)
new Userpassword();
class college
{ int
no;
String name;
String addr;
}
class Dept extends college
{ int
dno;
String dname; Scanner sc = new
Scanner(System.in); public void
accept()
{
name=sc.next();
dname=sc.next();
System.out.println("college name"+name);
System.out.println("college address"+addr);
}
public static void main(String a[])
{
Dept ob=new Dept();
ob.accept();
ob.display();
}
}
Slip12_2: Write a java program that works as a simple calculator. Use a grid layout to
arrange buttons for the digits and for the +, -, *, % operations. Add a text field to display
the result.
.
import java.awt.*; import
java.awt.event.*;
import javax.swing.*;
p1.add(equals); p1.add(add);
p1.add(sub); p1.add(mul);
p1.add(div); p1.add(clear);
add(p,BorderLayout.NORTH); add(p1);
if(f.isFile())
int ch,cnt=0;
while((ch=fis.read())!=-1)
{
if(ch=='\n')
cnt++;
}
}
import java.util.Date;
class slip13_2
{
public static void main(String[] args)
{
Date date = new Date();
SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
String strDate= formatter.format(date);
System.out.println(strDate);
===============================================================
Slip14_1: Write a program to accept a number from the user, if number is zero then throw user
defined exception “Number is 0” otherwise check whether no is prime or not (Use static keyword).
int no,i,j;
Scanner sc=new Scanner(System.in);
try
{
System.out.println("enter no");
no=sc.nextInt();
if(no==0)
throw new Zerono();
if(no>0)
{
for(i=2;i<=no/2;i++)
{
if(no%i==0)
{
count++;
}
}
}
if(count==0)
System.out.println("No is Prime");
else
this.ct=ct;
this.mt=mt;
this.et=et;
}
public void display()
{
System.out.println("\nMarks are;");
System.out.println("Computer\tMaths\tElectronics");
System.out.println(ct+"\t"+mt+"\t"+et);
System.out.println("Theory\tPracticals");
System.out.println(Theory+"\t"+Practicals);
}
}
Mainfile import
SY.SYMarks; import
TY.TYMarks;
import java.io.*;
class SYTY
{
int rollno;
String name;
while((ComputerTotal<0 || ComputerTotal>100))
System.out.println("\n\tInvalid marks.....");
ComputerTotal = Integer.parseInt(br.readLine());
}
System.out.println("\nEnter maths marks");
MathsTotal=Integer.parseInt(br.readLine());
while((MathsTotal<0 || MathsTotal>100))
{
System.out.println("\n\tInvalid marks.....");
CalculateGrade();
System.out.println("Result:");
System.out.println("Grade:A");
else if(percentage >= 60)
System.out.println("Grade:B");
class demoFile
{
public static void main(String args[]) throws Exception {
Scanner sc= new Scanner(System.in);
String f1=sc.next();
String f2=sc.next();
class Slip16_1 {
public static void main(String args[])
{
//you can read value from user
int a = 5;
Square s = (int x) -> x * x;
int ans = s.calculate(a);
System.out.println(ans);
}
}
Slip16_2: Write a program to design a screen using Awt that,
class MeEx
JMenu Edit;
JMenu About;
{
public static void main(String args[])
{
new MeEx();
}
}
import java.util.*;
class Customer {
String name;
int Pno;
}
class Depositer extends Customer
{
}
Slip17_2: Write Java program to design three text boxes and two buttons using swing. Enter
different strings in first and second textbox. On clicking the First command button,
concatenation of two strings should be displayed in third text box and on clicking second
command button, reverse of string should display in third text box
}
void put()
{
System.out.println("name: "+name);
System.out.println("no of innings: "+inning);
System.out.println("no of time(s) not out: "+tofnotout);
System.out.println("total runs: "+totalruns);
System.out.println("batting average: "+batavg);
} static void avg(int n, cricket
c[])
{
for(int i=0; i<n; i++)
{
c[i].batavg= c[i].totalruns/c[i].inning;
}
}
static void sort(int n, cricket c[])
{
String temp1;
int temp2, temp3, temp4;
float temp5;
for(int i=0; i<n; i++)
{
for(int j=i+1; j<n; j++)
{
if(c[i].batavg<c[j].batavg)
{
temp1= c[i].name;
c[i].name= c[j].name;
c[j].name= temp1;
temp2= c[i].inning;
c[i].inning= c[j].inning;
c[j].inning= temp2;
temp3= c[i].tofnotout;
Slip19_1: Write a program to accept the two dimensional array from user and display sum
of its diagonal elements.
}
}
Slip19_2: Write a program which shows the combo box which includes list of
T.Y.B.Sc.(Comp. Sci) subjects. Display the selected subject in a text field
Slip20_1: Write a Program to illustrate multilevel Inheritance such that country is inherited
from continent. State is inherited from country. Display the place, state, country and
continent.
import java.util.*;
class continent
{
String c1;
}
class country extends continent
{
String c2;
}
class state extends country
{
String s1;
String p1;
Slip20_2: Write a package for Operation, which has two classes, Addition and
Maximum. Addition has two methods add () and subtract (), which are used to add
two integers and subtract two, float values respectively. Maximum has a method
max () to display the maximum of two integers
}
public void add()
{
ans=n1+n2;
System.out.println("addition is="+ans);
}
else
System.out.println(n2+" is greater than "+n1);
import operation.Maximum;
import java.util.*;
class Arithmatic
{
public static void main (String args[])
{
int n1,n2; float num1,num2;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first no="); n1=sc.nextInt();
System.out.println("Enter second no="); n2=sc.nextInt();
System.out.println("Enter third no=");
num1=sc.nextFloat();
System.out.println("Enter fourth no=");
num2=sc.nextFloat();
ob1.add();
ob1.sub();
ob1.max();
{
System.out.println("Enter Date, Month and Year");
day=sc.nextInt(); if(day<1 ||
day>31) throw new
InvalidDateException();
month=sc.nextInt(); if(month>12
||month<1) throw new
InvalidDateException();
year=sc.nextInt();
if(year>10000 ||year<1000) throw
new InvalidDateException();
}
catch(InvalidDateException e)
{
System.out.println("Invalid Date entered");
System.exit(0);
}
catch(Exception e)
{
System.out.println("Enter Valid Date");
System.exit(0);
}
}
public void display()
{
System.out.println("Entered Date is "+day+":"+month+":"+year);
cnt=0;
Employee()
}
Employee(int id,String name,String deptname,double sal)
{
this.id=id;
this.name=name;
this.deptname=deptname;
this.sal=sal;
cnt++;
displayCount();
}
public static void displayCount()
{
System.out.println("Total Objects created "+cnt);
}
public void displayData()
{
System.out.println(this.id+"\t\t"+this.name+"\t\t\t"+this.deptname+"\t\t"+this. sal);
}
public static void main(String args[])
{
Employee e1=new Employee(101,"Maithili","HR",120020.20);
Slip22_1: Write a program to create an abstract class named Shape that contains two
integers and an empty method named printArea(). Provide three classes named
Rectangle, Triangle and Circle such that each one of the classes extends the class
Shape. Each one of the classes contain only the method printArea() that prints the
area of the given shape. (use method overriding).
class Rectangle extends Shape
{
Rectangle(int a,int b)
{
n1=a;
n2=b;
}
public void printArea()
{
float area;
area=n1*n2;
System.out.println("area of rectangle="+area);
}
}
class Triangle extends Shape
{
Triangle(int a,int b)
{
n1=a;
n2=b;
}
public void printArea()
{
{
ob.printArea();
Slip22_2: Write a program that handles all mouse events and shows the event name at
the center of the Window, red in color when a mouse event is fired. (Use adapter classes).
int x,y;
}
public void mouseEntered(MouseEvent me)
{
str+="Mouse Entered ";
this.display();
}
public void mouseExited(MouseEvent me)
{
str+="MouseExited"; this.display();
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
str+="MousePressed at: "+x+"\t"+y; this.display();
}
x=me.getX();
y=me.getY();
x=me.getX();
y=me.getY();
x=me.getX();
y=me.getY();
str+="Mouse Moved at:"+x+"\t"+y;
str="";
}
public static void main(String[] args) {
Slip23_1: Define a class MyNumber having one private int data member. Write a default
constructor to initialize it to 0 and another constructor to initialize it to a value (Use this).
Write methods isNegative, isPositive, isZero, isOdd, isEven. Create an object in main.Use
command line arguments to pass a value to the Object
import java.util.*;
class slip23_1
{
private int data;
slip23_1()
{
System.out.println(x+" Number is Negetive");
if(x%2==0)
{
System.out.println(x+" Number is Even");
}
}
public void isOdd(int x)
{
if(x%2!=0)
{
System.out.println(x+" Number is Odd ");
}
}
public void isZero(int x)
{
if(x==0)
{
System.out.println(x+" Number is Zero ");
}
}
public static void main(String args[])
{
int data=Integer.parseInt(args[0]);
1
class slip23_2 extends KeyAdapter
{
JFrame ob=new JFrame();
l1=new JLabel("US Dollars");
l2=new JLabel("Singapore Dollars");
l3=new JLabel("Euros");
t1=new JTextField(10);
t2=new JTextField(10);
t3=new JTextField(10);
ob.add(l1); ob.add(t1);
ob.add(l2); ob.add(t2);
ob.add(l3); ob.add(t3);
ob.setVisible(true);
t1.addKeyListener(this);
ob.setLayout(new FlowLayout());
ob.setSize(400,400);
ob.setDefaultCloseOperation(3);
}
{
System.out.println("Balance of BankC is "+bal);
}
public static void main(String args[])
{
BankA a=new BankA();
BankB b=new BankB();
BankC c=new BankC();
a.getBalance();
b.getBalance();
c.getBalance();
import java.awt.geom.*;
Slip25_1:Create a class Student(rollno, name ,class, per), to read student information from
the console and display them (Using BufferedReader class)
import java.io.* ;
class Student
{
InputStreamReader r=new InputStreamReader(System.in);
String number=br.readLine();
Slip25_2: Create the following GUI screen using appropriate layout manager.
Accept the name, class, hobbies from the user and display the selected options in
HobbiesDemo ()
{
l1=new JLabel("Your Name : ");
l2=new JLabel("Your Class :");
l3=new JLabel("Your Hobbies :");
l4=new JLabel(" ");
l5=new JLabel(" ");
tf1=new JTextField();
cb1=new JCheckBox("Music");
p1.setLayout(new GridLayout(1,2));
p2.add(rb1);
p2.add(rb2);
p2.add(rb3);
p3=new JPanel();
p3.setLayout(new GridLayout(4,1));
p3.add(l3);
p3.add(cb1);
p3.add(cb2);
p3.add(cb3); p4=new JPanel();
p4.setLayout(new GridLayout(1,2));
p4.add(l4);
p4.add(l5);
add(p1,BorderLayout.NORTH);
add(p2,BorderLayout.WEST);
add(p3,BorderLayout.EAST);
add(p4,BorderLayout.SOUTH);
setTitle("INFORMATION");
setSize(500,300);
if(cb2.isSelected())
s=s+cb2.getText()+" ";
Slip26_1: Define a Item class (item_number, item_name, item_price). Define a default and
parameterized constructor. Keep a count of objects created. Create objects using
parameterized constructor and display the object count after each object is created.(Use
static member and method). Also display the contents of each object
class Item
{
int ino;
String iname;
double iprice; static
int count=0;
Item()
{ }
Item(int no,String nm,double d)
{
ino=no;
nm=iname;
iprice=d;
count++;
}
public void display()
{
System.out.println("Total objects created "+count);
System.out.println(ino+" "+iname+" "+iprice);
}
public static void main(String args[])
{
Item ob1=new Item(1,"Laptop",20000.00);
ob1.display();
Item ob2=new Item(1,"Laptop",20000.00);
ob2.display();
}
}
Slip26_2: Define a class ‘Donor’ to store the below mentioned details of a blood donor.
name, age, address, contactnumber, bloodgroup, date of last donation. Create ‘n’ objects of
this class for all the regular donors at Pune. Write these objects to a file. Read these objects
from the file and display only those donors’ details whose blood group is ‘A+ve’ and had
not donated for the recent six months.
class Donor
{
String name, address,group;
int age, contact, lod;
catch(IOException e)
{
for(int i=0;i<n;i++)
{
Donor d=(Donor)z.readObject(); if(d.group.equals("A+ve")&&d.lod>=6)
System.out.println(d);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Slip27_1: Define an Employee class with suitable attributes having getSalary() method,
which returns salary withdrawn by a particular employee. Write a class Manager which
extends a class Employee, override the getSalary() method, which will return salary of
manager by adding traveling allowance, house rent allowance etc.
import java.util.*;
class Employee
{
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 for Employee Salary and 0 for Manager");
int ch=sc.nextInt(); if(ch==1)
{
Employee Eob=new Employee();
Eob.getSalary();
}
else if(ch == 0)
{
Manager Mob=new Manager();
Mob.getSalary();
}
else
System.out.println("Entered Wrong Choice");
}
}
Slip27_2: Write a program to accept a string as command line argument and check
whether it is a file or directory. Also perform operations as follows:
i) If it is a directory,delete all text files in that directory. Confirm delete
operation from user before deleting text files. Also, display a count showing
the number of files deleted, if any, from the directory. ii) If it is a file display
various details of that file.
if(n==1)
{
String[] s1=f.list();
String a=".txt";
for(String str: s1)
{
System.out.println(str);
if(str.endsWith(a))
{
File f1=new File(fname, str);
System.out.println(str+"-->Deleted");
f1.delete();
}
}
}
else
System.out.println("OKKKK");
}
}
}
Slip28_1: Write a program that reads on file name from the user, then displays information
about whether the file exists, whether the file is readable, whether the file is writable, the
type of file and the length of the file in bytes.
if(f.canRead())
else
System.out.println("File is Writeable");
else
import java.util.Scanner;
class Customer
{
int cno;
String cname,cmob,cadd;
Slip29_2: Write a program to create a super class Vehicle having members Company and
price.
Derive two different classes LightMotorVehicle(mileage) and HeavyMotorVehicle
(capacity_in_tons). Accept the information for "n" vehicles and display the
information in appropriate form. While taking data, ask user about the type of
vehicle first.
}
public void display()
{
System.out.println("Mileage: "+mileage);
}
}
for(i=0;i<m;i++){
h[i].display();
}
break;
}
}
import java.util.*;
class person
{
String Personname;
int Adharno, Panno;
Slip30_2: Write a program that creates a user interface to perform integer divisions.
The user enters two numbers in the text fields, Number1 and Number2. The division of
Number1 and Number2 is displayed in the Result field when the Divide button is clicked. If
Number1 or Number2 were not an integer, the program would throw a
NumberFormatException. If Number2 were Zero, the program would throw an
Arithmetic Exception Display the exception in a message dialog
box
import javax.swing.*;
import java.awt.*; import
java.awt.event.*;
class Division extends JFrame implements ActionListener
{
JLabel l1,l2,Result;
setLayout(new FlowLayout());
btn1.addActionListener(this);
setVisible(true);
setSize(300,400);
int v1=Integer.parseInt(t1.getText());
int v2=Integer.parseInt(t2.getText());
int ans=v1/v2;
Result.setText(ans+" ");
}
catch(NumberFormatException e)
{
System.out.println("Enter a valid number!");
}
catch(ArithmeticException o)
{
System.out.println("Divided by zero");
}
}
public static void main(String args[])
{
Division ob1=new Division();
}
}
TO JOIN NR CLASSES LLP
WhatsApp on
9730381255
Follow us on Instagram
@logic_overflow