Practical Assignment: SYBCA-DIV-1-SEM-4 Java Roll No:84

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

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);

System.out.print("Enter Two Numbers : ");


first = scanner.nextInt();
second = scanner.nextInt();

add = first + second;

subtract = first - second;


multiply = first * second;
devide = (float) first / second;

System.out.println("Sum = " + add);


System.out.println("Difference = " + subtract);
System.out.println("Multiplication = " + multiply);
System.out.println("Division = " + devide);
}
}

Output:

1
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

Enter two Numbers:90

50

Sum:140

Difference:40

Multiplication:4500

Division:1.8

Q3.Create STACK class with push, pop fun.


import java.io.*;

import java.util.*;

class P14

static void stack_push(Stack<Integer> stack)

for(int i = 0; i < 5; i++)

stack.push(i);

static void stack_pop(Stack<Integer> stack)

System.out.println("Pop Operation:");

for(int i = 0; i < 5; i++)

Integer y = (Integer) stack.pop();

System.out.println(y);

public static void main (String[] args)

Stack<Integer> stack = new Stack<Integer>();

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;

public class P19 {

int empid;

String name;

float salary;

public void getInput() {

Scanner in = new Scanner(System.in);

System.out.print("Enter the empid :: ");

empid = in.nextInt();

System.out.print("Enter the name :: ");

name = in.next();

System.out.print("Enter the salary :: ");

salary = in.nextFloat();

public void display() {

System.out.println("Employee id = " + empid);

System.out.println("Employee name = " + name);

3
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

System.out.println("Employee salary = " + salary);

public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.print("Enter number of employees: ");

int n = in.nextInt();

P19 e[] = new P19[n];

for (int i = 0; i < n; i++) {

e[i] = new P19();

e[i].getInput();

System.out.println("\n\n**** Data Entered as below ****");

for (int i = 0; i < n; i++) {

e[i].display();

Output:

Enter number of employees: 4

Enter the empid :: 1

Enter the name :: JAY

Enter the salary :: 20000

Enter the empid :: 2

Enter the name :: shubham

Enter the salary :: 25000

Enter the empid :: 3

Enter the name :: prince

Enter the salary :: 30000

Enter the empid :: 4

Enter the name :: Ajay

4
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

Enter the salary :: 35000

**** Data Entered as below ****

Employee id = 1

Employee name = JAY

Employee salary = 20000.0

Employee id = 2

Employee name = shubham

Employee salary = 25000.0

Employee id = 3

Employee name = prince

Employee salary = 30000.0

Employee id = 4

Employee name = Ajay

Employee salary = 35000.0

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.

abstract class Shape

private double height;

private double width;

public void setValues(double height, double width)

this.height = height;

this.width = width;

public double getHeight()

return height;

5
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

public double getWidth()

return width;

public abstract double getArea();

class Rectangle extends Shape

public double getArea()

return getHeight() * getWidth();

class Triangle extends Shape

public double getArea()

return (getHeight() * getWidth()) / 2;

class main

public static void main(String[] args)

Shape shape;

Rectangle r = new Rectangle();

shape = r;

shape.setValues(40, 9);

System.out.println("Area of rectangle : " + shape.getArea());

Triangle t = new Triangle();

shape = t;

shape.setValues(75,16);

System.out.println("Area of triangle : " + shape.getArea());

6
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

Output:

Area of rectangle : 360.0

Area of triangle : 600.0

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

public static void main(String[] args)

String input = "Siddharth Gohil";

StringBuilder inputstring = new StringBuilder();

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

int first_in = str.indexOf('s', 10);


System.out.println("First occurrence of char 's'" +" after index 10 : " + first_in);
int last_in = str.lastIndexOf('s', 20);
System.out.println("Last occurrence of char 's'" +" after index 20 is : " + last_in);
int char_at = str.charAt(20);
System.out.println("Character at location 20: " + char_at);
}
}

Output:

First occurrence of char 's' is found at : -1


Last occurrence of char 's' is found at : -1
First occurrence of char 's' after index 10 : -1
Last occurrence of char 's' after index 20 is : -1
Character at location 20: 32

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:

Enter the name:


siddharth
Enter the account number:
2626
Enter the account balance:
40000

**** Account Details ****


Name: siddharth
Account number:2626
Account balance:40000

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("Inside try block");


}
catch(ArrayIndexOutOfBoundsException ex)

{
System.out.println("Exception caught in catch block");
}
finally
{
System.out.println("finally block executed");

System.out.println("Outside try-catch-finally clause");


}
}

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

String[] string = new String [sc.nextInt()];


sc.nextLine();
for (int i = 0; i<string.length; i++)
{
string[i] = sc.nextLine();
}
System.out.println("\nYou have entered: ");
for(String str: string)
{
System.out.println(str);
}
}
}

Output:

Please enter the number of strings you want to enter:3


Hello
How are you
Have a good day

You have entered:


Hello
How are you
Have a good day

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;

public class CircleInsideTriangle extends Applet


{
public void paint(Graphics g)
{
int xPoints[] = {120, 220, 30};
int yPoints[] = {30, 220, 220};
g.setColor(Color.BLACK);
g.fillPolygon(xPoints, yPoints, 3);
g.setColor(Color.PINK);
g.fillOval(90, 120, 70, 70);
g.drawString("alis",140,150);
}
}
Html file:
<html>
<applet code="appletDigitalClock"width=400 height=400>
</applet>
</html>

Output:

10
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

Q-12. Write an applet program which show Digital Clock.


ANS:-
import java.applet.*;
import java.awt.*;
import java.util.*;
import java.text.*;

public class DigitalClock extends Applet implements Runnable {

Thread t = null;
int hours=0, minutes=0, seconds=0;
String timeString = public void init() {
setBackground( Color.pink);
}

public void start() {


t = new Thread( this );
t.start();
}

public void run() {


try {
while (true) {

Calendar cal = Calendar.getInstance();


hours = cal.get( Calendar.HOUR_OF_DAY );
if ( hours > 12 ) hours -= 12;
minutes = cal.get( Calendar.MINUTE );
seconds = cal.get( Calendar.SECOND );

SimpleDateFormat formatter = new SimpleDateFormat("hh:mm:ss");

11
SYBCA-DIV-1-SEM-4 JAVA ROLL NO:84

Date date = cal.getTime();


timeString = formatter.format( date );

repaint();
t.sleep( 1000 ); // interval given in milliseconds
}
}
catch (Exception e) { }
}

public void paint( Graphics g ) {


g.setColor( Color.blue );
g.drawString( timeString, 50, 50 );
}
}

Html file:
<html>
<body>
<applet code="DigitalClock"width="100"height="100">
</applet>
</body>
</html>

Output:

12

You might also like