JAVAASSINEW2
JAVAASSINEW2
Write a Java code to find the sum of any number of integers entered as command line arguments.
Output:
ii)Buffered Reader
iii)Scannner
import java.util.Scanner;
class fact
int n,fact=1;
n=r.nextInt();
for(int i=1;i<=n;i++)
fact=fact*i;
System.out.print("factorial "+fact);
ii)Source code:-
import java.io.*;
class Factorial1
InputStreamReader(System.in));
int a = 0, f = 1;
try
a = Integer.parseInt(bf.readLine());
catch (Exception e)
e.printStackTrace();
if (a == 0 || a == 1)
else
f = f * i;
Output:-
iii)Source code:-
import java.util.Scanner;
num = s.nextInt();
fact = fact * i;
Output:-
Enter a Number: 6
INPUT
import java.util.Scanner;
class A
n1 = r.nextInt();
n2 = r.nextInt();
for(j=2;j<=i;j++)
if(i%j==0)
break;
if(i==j)
System.out.print(j+" ");
OUTPUT
50
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
4 Write a Java code to take ten numbers as input and sort them is ascending order.
INPUT
num[i] = scan.nextInt();
}
scan.close();
for (int i = 0; i < count; i++)
{
for (int j = i + 1; j < count; j++)
{
if (num[i] > num[j])
{
temp = num[i];
num[i] = num[j];
num[j] = temp;
}
}
}
System.out.print("Array Elements in Ascending Order: ");
for (int i = 0; i < count - 1; i++)
{
System.out.print(num[i] + ", ");
}
System.out.print(num[count - 1]);
}
}
Output:-
Enter number of elements you want in the array: 10
Enter array elements:
3
5
8
9
1
12
45
34
21
67
Array Elements in Ascending Order: 1, 3, 5, 8, 9, 12, 21, 34, 45, 67
5.Write a Java code to take Account no. Name and
Balance as input of ten customers and display the name and
account no. of those customers whose balance is less than one
thousand.
Source code:-
import java.util.Scanner;
public class CustomerAccounts
{
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int numberOfCustomers = 10;
String[] accountNumbers = new String[numberOfCustomers];
String[] names = new String[numberOfCustomers];
double[] balances = new double[numberOfCustomers];
for (int i = 0; i < numberOfCustomers; i++)
{
System.out.println("Enter details for Customer " + (i + 1) + ":");
System.out.print("Account Number: ");
accountNumbers[i] = scanner.nextLine();
System.out.print("Name: ");
names[i] = scanner.nextLine();
System.out.print("Balance: ");
balances[i] = scanner.nextDouble();
scanner.nextLine();
System.out.println();
}
System.out.println("Customers with a balance less than 1000:");
}
private void calculateTriangleArea(double side1, double side2, double side3)
{
double s = (side1 + side2 + side3) / 2;
surfaceArea = Math.sqrt(s * (s - side1) * (s - side2) * (s - side3));
}
private void calculateSquareArea(double side)
{
surfaceArea = side * side;
}
public double getSurfaceArea()
{
return surfaceArea;
}
public static void main(String[] args)
{
Figure circle = new Figure(5.0); // Creating a circle object with radius
5.0
System.out.println("Circle Surface Area: " + circle.getSurfaceArea());
Figure triangle = new Figure(3.0, 4.0, 5.0); // Creating a triangle object
with sides 3.0, 4.0, and 5.0
System.out.println("Triangle Surface Area: " +
triangle.getSurfaceArea());
Figure square = new Figure(7.0); // Creating a square object with side
7.0
System.out.println("Square Surface Area: " + square.getSurfaceArea());
}
}
Output:-
Circle Surface Area: 78.53981633974483
Triangle Surface Area: 6.0
Square Surface Area: 153.93804002589985
Output:-
INPUT
if (str.toLowerCase().equals(reverseStr.toLowerCase())) {
else {
OUTPUT
10. Write a java code to show that String class is immutable and StringBuffer class is mutable.
INPUT:
if (x == y) {
} else {
referenceCheck(st1, st2);
st1 += "ava";
System.out.println("After Modification");
referenceCheck(st1, st2); }
Output:
Rs. 1000.
Source code:-
import java.util.Scanner;
class Account
accNo=num;
bal=rs;
bal+=am;
InsufficientBalanceException
{
double newBal=bal-am;
if(newBal<1000)
bal=newBal;
:");
int n=sc.nextInt();
double b=sc.nextDouble();
acc.setAccount(n,b);
double am=sc.nextDouble();
acc.deposit(am);
double with=sc.nextDouble();
try
acc.withdrawal(with);
catch(InsufficientBalanceException e)
System.out.println(e.getMessage());
}
}
Output:-
2200336
1500
500
1200
12-Write a Java code to create a package named “testpackage”, create a class named “Add” under this package
which contains a method named ”display”. Import this package in another Java program.
Source code:-
package testpackage;
'testpackage'.");
import testpackage.Add;
Output:-
INPUT
class Outer
void print()
System.out.println(x);
Output:-
Source code:-
import java.io.*;
System.out.println(innerClass.innerVariable);
o.accessInnerVariable();
Output:-
14.Write a Java code to create Interface. Implement this Interface in another Java Program.
Source code:-
void display();
}
public static void main(String[] args)
obj.display();
Output:-
INPUT
System.out.print("sumit");
class B
A t=new A();
t.start();
System.out.print("mahato");
OUTPUT
mahatomahatomahatomahatomahatosumitsumitsumitsumitsumit
Question 16:-Write an Applet code to draw lines, rectangles, ovals and arcs.
Source code:-
import java.awt.*;
import java.applet.*;
{ g.drawLine(50,50,250,50);
g.drawRect(50,100,250,100);
g.drawOval(50,250,150,100);
g.drawArc(50,400,150,100,45,270);
}}
OUTPUT:
17.Write an Applet code to implement passing parameters in Applet using “PARAM” tag.
INPUT:
Import java.awt*;
Import java.applet.*;
String str;
str= getParameter(“string”);
if(str==null)
str=”java”;
g.drawstring(str,10,100)
<HTML>
<HEAD>
</HEAD>
<BODY>
<APPLET CODE=”HelloJavaParam.class
WIDHT=400
HEIGHT=200>
VALUE=”Applet!”>
</APPLET>
</BODY>
</HTML>
OUTPUT
INPUT
import java.awt.*;
import java.applet.*;
private Image i;
i=getImage(getDocumentBase(),"Screenshot_2023-07-15-10-07-46-
19_99c04817c0de5652397fc8b56c3b3817.jpg");
g.drawImage(i,100,100,500,400,this);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Applet Image</title>
</head>
Page 42 of 48
<body>
</applet>
</body>
</html>
OUTPUT
import java.applet.*;
import java.net.*;
{ URL url=getClass().getResource("feluda-harmonica.wav");
audio=getAudioClip(url); }
public void start()
{ audio.play(); }
public void stop()
{ audio.stop(); } }
OUTPUT
INPUT
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
Button b1;
TextField tf1,tf2,tf3;
Label lb1,lb2,lb3;
lb1.setBounds(100,40,100,20);
tf1.setBounds(210,40,100,20);
lb2.setBounds(100,80,100,20);
lb3.setBounds(100,120,100,20);
tf3.setBounds(210,120,100,20);
b1=new Button("speed");
Page 46 of 48
b1.setBounds(180,160,60,50);
add(lb1);
add(lb2);
add(lb3);
add(tf1);
add(tf2);
add(tf3);
add(b1);
b1.addActionListener(this);
setLayout(null);
int a = Integer.parseInt(tf1.getText());
int b = Integer.parseInt(tf2.getText());
float speed=0;
if(e.getSource() == b1)
speed=a/b;
tf3.setText(String.valueOf(speed));
OUTPUT