Practical Assignment: SYBCA-DIV-1-SEM-4 Java Roll No:84
Practical Assignment: SYBCA-DIV-1-SEM-4 Java Roll No:84
Practical Assignment
Q-1. Addition of two values passing by command line argument.
ANS:-
public class sum
{
public static void main(String[] args)
{
int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
int sum = a+b;
System.out.println("sum is = " +sum);
}
}
Output:
5+10=15
Q2. Make CALC class with two data members. Perform addition, subtraction,
multiplication and division operation of given two values by user. Initialize both data
members with zero. (use menu-driven)
import java.util.Scanner;
public class P7
{
public static void main(String args[])
{
int first, second, add, subtract, multiply;
float devide;
Scanner scanner = new Scanner(System.in);
Output:
1
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
50
Sum:140
Difference:40
Multiplication:4500
Division:1.8
import java.util.*;
class P14
stack.push(i);
System.out.println("Pop Operation:");
System.out.println(y);
stack_push(stack);
2
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
stack_pop(stack);
Output:
Q.4 Create class EMPLOYEE with ͚id, name and salary͛ private data-members. Create 5
objects dynamically through user input. Create two methods which display data salary
wise and name wise.
import java.util.Scanner;
int empid;
String name;
float salary;
empid = in.nextInt();
name = in.next();
salary = in.nextFloat();
3
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
int n = in.nextInt();
e[i].getInput();
e[i].display();
Output:
4
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
Employee id = 1
Employee id = 2
Employee id = 3
Employee id = 4
Q5. Create abstract class Figure and its child classes Rectangle and Triangle. Figure
class has abstract area method which is implemented by Rectangle and Triangle class to
calculate area. Use run time polymorphism to calculate area or Rectangle and Triangle
objects.
this.height = height;
this.width = width;
return height;
5
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
return width;
class main
Shape shape;
shape = r;
shape.setValues(40, 9);
shape = t;
shape.setValues(75,16);
6
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
Output:
Q6. Write a java code which accept a string and display the string in reverse order.
import java.lang.*;
import java.io.*;
import java.util.*;
class Reversestring
inputstring.append(input);
inputstring.reverse();
System.out.println(inputstring);
Output:
lihoG htrahddiS
Q7.Write a program for searching a given sub string from the given sentence. Also calculate number of
times given sub string occur in given sentence.
import java.io.*;
class main
{
public static void main (String[] args)
{
String str = "You Are Very Special For Me";
int firstIndex = str.indexOf('s');
System.out.println("First occurrence of char 's'" +" is found at : " + firstIndex);
int lastIndex = str.lastIndexOf('s');
System.out.println("Last occurrence of char 's' is" +" found at : " + lastIndex);
7
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
Output:
Q8. Write a program to make a package Balance in which has Account class with
Display_Balance method in it. Import Balance package in another program to access
Display_Balance method of Account class.
package p1;
import java.io.*;
class account
{
long acc,bal;
String name;
public void read()throws Exception
{
DataInputStream in=new DataInputStream(System.in);
System.out.println("Enter the name:");
name=in.readLine();
System.out.println("Enter the account number :");
acc=Long.parseLong(in.readLine());
System.out.println("Enter the account balance :");
bal=Long.parseLong(in.readLine());
}
public void disp()
{
System.out.println("**** Account Details ****");
System.out.println("Name :"+name);
System.out.println("Account number :"+acc);
System.out.println("Account Balance :"+bal);
}
}
class main
{
public static void main(String ar[])
{
try
{
balance.account ob=new balance.account();
ob.read();
ob.disp();
}
catch(Exception e)
{
8
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
System.out.println(e);
}
}
}
Output:
Q9. Create your own User defined exception InvalidCharException if any one of the
char @,*,? is present in given string.
class GFG
{
public static void main (String[] args)
{
// array of size 4.
int[] arr = new int[4];
try
{
int i = arr[4];
{
System.out.println("Exception caught in catch block");
}
finally
{
System.out.println("finally block executed");
Q-10. Write a program to accept 10 names from the user and print all the names which start from B with
interval of 2 sec.
ANS:-
import java.util.Scanner;
public class MultipleStringInputExample1
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Please enter the number of strings you want to enter: ");
9
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
Output:
Q-11. create an applet program which display triangle having circle. Circle have to
touch with all edges of triangle. Also write your name between circle.
ANS:-
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
Output:
10
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
Thread t = null;
int hours=0, minutes=0, seconds=0;
String timeString = public void init() {
setBackground( Color.pink);
}
11
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84
repaint();
t.sleep( 1000 ); // interval given in milliseconds
}
}
catch (Exception e) { }
}
Html file:
<html>
<body>
<applet code="DigitalClock"width="100"height="100">
</applet>
</body>
</html>
Output:
12