Escolar Documentos
Profissional Documentos
Cultura Documentos
3. Write a class Student (name, rollno). Inherit a class Test (marks) from Student. Write
an interface Sports having a function PlayedSports() that inputs the number of sports
played by a student. Write a class Result that inherits from Test and Sports and
display the final result.
4. Program to input name, account type, account number and balance from user and
store it in a class which has two functions withdraw – to withdraw amount from
balance, and deposit – to deposit amount to the account. Do appropriate exception
handling.
a. Copy.
c. Reverse.
6.
7. Write a program to implement a calculator. Ask for inputs from user at runtime using
try and catch.
8. Create a class Vehicle. Inherit a public class Car. Create a public interface Fly. Create
a public interface PositioningService. Create a class HifiCar which extends from Car
and implements Fly and PositioningService. Make use of appropriate functions and
show following concepts:
a. Constructors.
b. Constructor overloading.
c. Function Overriding.
d. Inheritance.
e. Interfaces.
9. Create a class Vehicle. Inherit a public class Car. Create a public interface Fly. Create
a public interface PositioningService. Create a package of the two interfaces. Create a
class HifiCar which extends from Car and implements Fly and PositioningService.
Make use of appropriate functions and show following concepts:
a. Constructors.
b. Constructor overloading.
c. Function Overriding.
d. Inheritance.
e. Interfaces.
f. Packages.
13. Write a program that reads a string composed of 12 characters then splits it into 4
different strings each composed of three characters. Print the resulting strings each on
a separate line.
14. Write a program that reads a string s then another string p contained in s. Then form 3
strings extracted from s: the first part of s that does not contain p, the part that
contains p and the last part. Print each of the three strings with its length.
16. Write an interface ConvertTemperature. Implement two classes CtoF and FtoC to
conv9oert the temperatures. Make it menu driven.
20. Write a program of applets which displays a string in various fonts and colour formats
randomly.
Q1
import java.util.*;
class find1
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
int r=1,i,j,result=1;
while(r>0)
{
System.out.println("*****MENU*****:\n1.Factorial\n2.ODD or
EVEN\n3.Pattern\n4.Exit");
System.out.println("Enter choice : ");
int n= sc.nextInt();
switch(n)
{
case 1:
{
System.out.println("Enter number = ");
int no1 = sc.nextInt();
for(i=no1;i>=1;i--)
{
result=result*i;
}
System.out.println("The Factorial of the number "+no1+" is
"+result);
break;
}
case 2:
{
System.out.println("Enter number = ");
int no2 = sc.nextInt();
if(no2%2==0)
{
System.out.println("The number "+no2+" is even");
}
else
{
System.out.println("The number "+no2+" is odd");
}
break;
}
case 3:
{
System.out.println("Enter number of lines = ");
int no3 = sc.nextInt();
for(i=0;i<=no3;i++)
{
for(j=0;j<i;j++)
{System.out.print(i+" ");}
System.out.print("\n");
}
break;
}
case 4:
{
r--;
}
}
}
}
}
Q2
class array2a
{
public static void main(String args[])
{
int no[]={55,68,36,12,95,25};
int n=no.length;
System.out.println("Given list");
for(int i=0;i<n;i++)
{
System.out.print(" "+no[i]);
}
System.out.print("\n ");
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
int temp;
if(no[i]<no[j])
{
temp=no[i];
no[i]=no[j];
no[j]=temp;
}
}
}
for(int i=0;i<n;i++)
{
System.out.print(" "+no[i]);
}
}
}
------
Q4
import java.util.*;
class bank4
{
String name;
String atype;
int accno;
double bal;
bank4(String n, String at, int ac, double b)
{
name=n;
atype=at;
accno=ac;
bal=b;
}
public void withdraw(double w)
{
try
{
if(w>bal)
throw new Exception();
bal=bal-w;
display();
System.out.println("The Transaction has occured successfully");
}
catch(Exception e)
{
System.out.println("You don't have enough balance!");
}
}
public void deposit(double d)
{
bal=bal+d;
display();
System.out.println("Amount successfully deposited");
}
public void display()
{
System.out.println("Name = "+name+"\nAccount Type = "+atype+"\nAccount
Number = "+accno+"\nBalace = "+bal);
}
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
int n=0;
bank4 a=new bank4("Anuj","Saving",12654,100000.45);
a.display();
while(n==0)
{
System.out.println("*****Menu*****\n1.Withdraw\n2.Deposit\n3.Account
Information\n4.Exit\nEnter choice = ");
int ch=sc.nextInt();
switch(ch)
{
case 1:
{
System.out.print("Enter the amount to be withdrawn = ");
double w=sc.nextDouble();
a.withdraw(w);
break;
}
case 2:
{
System.out.print("Enter the amount to deposit = ");
double d=sc.nextDouble();
a.deposit(d);
break;
}
case 3:
{
a.display();
break;
}
case 4:
{
System.out.println("Thank you. Visit again.");
n++;
break;
}
}
}}
}
Q7
import java.util.*;
class cal7
{
public static void main(String arg[])
{
Scanner sc = new Scanner(System.in);
int x,y,z;
int r=0;
System.out.println("Enter the 1st operand = ");
x=sc.nextInt();
System.out.println("Enter the 2nd operand = ");
y=sc.nextInt();
while(r==0)
{
try
{
System.out.println("Select operator +,*,/,-, x for exit ");
String b= sc.next();
char ch= b.charAt(0);
switch(ch)
{
case '+':
z=x+y;
System.out.println("Result = "+z);
break;
case '-':
z=x-y;
System.out.println("Result = "+z);
break;
case '*':
z=x*y;
System.out.println("Result = "+z);
break;
case '/':
z=x/y;
System.out.println("Result = "+z);
break;
case 'x':
r++;
break;
default:
throw new Exception("Wrong operator");
}
}
catch (Exception e)
{
System.out.println(e);
}
}}
Q10
<APPLET
CODE=myApplet.class
WIDTH=250
HEIGHT=250
>
</APPLET>
import java.applet.*;
import java.awt.*;
class myApplet10 extends Applet
{
public void paint(Graphics g)
{
g.setColor(Color.orange);
g.draw3DRect(100,100,50,50,true);
g.setColor(Color.green);
g.fill3DRect(100,100,50,50,true);
}
}
Q3
import java.util.*;
interface sport
{
public void playedsport(int a);
}
class student
{
public int roll;
public String name;
student()
{
roll=0;
name=null;
}
}
void show()
{System.out.println("Car details:\nType: "+type+"\nCode: "+code);
}
}
Q11
import java.io.*;
class stacks
{ public static void main(String args[])throws IOException
{
InputStreamReader inStream = new InputStreamReader(System.in);
BufferedReader pen = new BufferedReader(inStream);
System.out.println("Enter the size of stack");
String d1=pen.readLine();
int n=Integer.parseInt(d1);
int top=-1,f=-1,ch;
int st[]=new int[n];
try
{
do
{ System.out.println("1.Push\n2.Pop3.\nexit");
System.out.println("Enter choice:");
String d2=pen.readLine();
ch=Integer.parseInt(d2);
switch(ch)
{
case 1:
String d3=pen.readLine();
int x=Integer.parseInt(d3);
if(top>n)
throw new Exception("Stack is full");
else
st[++top]=x;
break;
case 2:
System.out.println("Element removed:"+st[top--]);
break;
default:System.out.println("wrong choice");
}
}while(ch!=3);
}
catch(Exception e)
{
System.out.println(e);
}
}
}
Q12
import java.io.*;
class Queues
{public static void main(String args[])throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("Enter size of queue");
n=Integer.parseInt(br.readLine());
int arr[]=new int[n];
int front=0,rear=-1,flag=-1;
int x;
try
{while(flag!=2)
{System.out.println("Enter element to be inserted in queue");
x=Integer.parseInt(br.readLine());
if(rear==n-1)
throw new Exception("queue is full");
else
arr[++rear]=x;
System.out.println("Do you want to continue?1-Yes, 2-No\nEnter choice: ");
flag=Integer.parseInt(br.readLine());
}
}catch(Exception e)
{System.out.println(e);
}
while(front!=rear)
System.out.println(arr[front++]);
}
}
Q13
class Split
{String s;
Split(String a)
{s=a;}
void sub(int a,int b)
{String substr="";
for(int i=a;i<b;i++)
substr=substr+ s.charAt(i);
System.out.println(substr);
}
}
class Stringsplit0
{public static void main(String args[])
{
Split s1=new Split("Hello There!");
System.out.println("Main string: "+s1);
System.out.println("Substrings:");
s1.sub(0,3);
s1.sub(3,6);
s1.sub(6,9);
s1.sub(9,12);
}
}
Q14
import java.io.*;
class StrFunc
{String s;
StrFunc(String a)
{s=a;}
boolean starts(String a,int b)
{boolean result=false;
for(int i=0;i<a.length();i++)
{if(s.charAt(i)==a.charAt(i))
result=true;
else
{result=false;
break;
}
}return result;
}
void sub(int a,int b)
{String substr="";
for(int i=a;i<b;i++)
substr=substr+ s.charAt(i);
System.out.println(substr);
}
}
class TwoStrings
{public static void main(String args[]) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the two strings");
String s1=br.readLine();
String p=br.readLine();
StrFunc s=new StrFunc(s1);
int i=-1;
for(int x=0;x<s1.length();x++)
{if(s1.charAt(x)==p.charAt(0))
{if(s.starts(p,i)!=true)
i=x;
}
}
if(i>=0)
{System.out.println("Broken strings:");
s.sub(0,i);
s.sub(i,i+p.length());
s.sub(i+p.length(),s1.length());
}else
System.out.println("Second string not present in first string");
}
}
Q15
import java.io.*;
class Quadratic
{ public static void main(String args[]) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
double a,b,c,root1,root2;
System.out.println("Enter values of the quadratic equation");
a=Double.parseDouble(br.readLine());
b=Double.parseDouble(br.readLine());
c=Double.parseDouble(br.readLine());
root1=(-b+Math.sqrt((b*b - 4*a*c)))/(2*a);
root2=(-b-Math.sqrt((b*b - 4*a*c)))/(2*a);
System.out.println("The roots are : "+root1+" and "+root2);
}
}
Q16
interface ConvertTemp
{
public void convert();
}
class CtoF implements ConvertTemp
{
float tempc, tempf;
CtoF(float t)
{tempc = t;
}
public void convert()
{
tempf = (((9/5)*tempc)+32);
System.out.println("Temp in C = "+tempc);
System.out.println("Temp in F = "+tempf);
}
}
class FtoC implements ConvertTemp
{
float tempc, tempf;
FtoC(float t)
{tempf = t;
}
public void convert()
{
tempc = ((9/5)*(tempf-32));
System.out.println("Temp in C = "+tempc);
System.out.println("Temp in F = "+tempf);
}
}
public class Converter
{
public static void main(String [] args)
{
CtoF a = new CtoF(15);
a.convert();
FtoC b = new FtoC(95);
b.convert();
}
}
Q17
Matrix
Q18
import java.io.*;
class CompareNames
{String s;
CompareNames(String str)
{s=str;}
int comparestr(String a)
{int res=-1;
for(int i=0;i<s.length()&&i<a.length();i++)
{res=s.charAt(i)-a.charAt(i);
if(res!=0)
break;
}
if(res==0&&s.length()<a.length())
res=-1;
else if(res==0&&s.length()>a.length())
res=1;
return res;
}
}
class SortNames
{public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n;
System.out.println("\nEnter number of names");
n=Integer.parseInt(br.readLine());
String names[]=new String[n];
System.out.println("Enter names");
for(int i=0;i<n;i++)
names[i]=br.readLine();
String temp;
for(int i=n-1;i>0;i--)
{for(int j=0;j<i;j++)
{CompareNames c1=new CompareNames(names[j]);
if(c1.comparestr(names[j+1])>0)
{temp=names[j];
names[j]=names[j+1];
names[j+1]=temp;
}
}
}
System.out.println("\nSorted array of names:");
for(int i=0;i<n;i++)
System.out.println(names[i]);
}
}
Q19
import java.io.*;
class UppertoLower
{ public void convertstring(String s)
{String s1="";
for(int i=0;i<s.length();i++)
{if(s.charAt(i)>='A'&&s.charAt(i)<='Z')
s1=s1+(char)(s.charAt(i)+32);
else
s1=s1+s.charAt(i);
}
System.out.println("Converted String : "+s1);
}
}
class LowertoUpper
{public void convertstring(String s)
{String s1="";
for(int i=0;i<s.length();i++)
{if(s.charAt(i)>='a'&&s.charAt(i)<='z')
s1=s1+(char)(s.charAt(i)-32);
else
s1=s1+s.charAt(i);
}
System.out.println("Converted String: "+s1);
}
}
class CaseConverter
{public static void main(String args[]) throws IOException
{BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
UppertoLower obj1=new UppertoLower();
LowertoUpper obj2=new LowertoUpper();
System.out.println("Menu\n1.Upper to Lower\n2.Lower to Upper\nChoice : ");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{case 1: obj1.convertstring("THIS IS IN UPPER CASe");
break;
case 2: obj2.convertstring("this is in Lower case");
break;
default:System.out.println("Wrong choicee!");
}
}
}
Q20
import java.applet.Applet;
import java.awt.*;
/*<applet code="Font" width=200 height=50>
</applet>*/
public class Font extends Applet
{public void init()
{setBackground(Color.cyan);
}
public void paint(Graphics g)
{
g.setColor(Color.RED);
g.drawString("This is",10,20);
//g.setFont("Times New Roman");
g.setColor(Color.blue);
g.drawString("a tes",10,29);
// g.setFont("Comic Sans MS");
g.setColor(Color.black);
g.drawString("t program",10,36);
}
}