0% found this document useful (0 votes)
10 views16 pages

Core Java Practical Solutions for BBA

The document contains practical solutions for a Core Java course, covering various programming tasks including arithmetic operations, applet applications, string manipulations, recursion, and file handling. Each task is presented with a Java code solution, demonstrating concepts such as command line arguments, event handling, and method overloading. The document serves as a comprehensive guide for students to understand and implement Java programming techniques.

Uploaded by

shivani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views16 pages

Core Java Practical Solutions for BBA

The document contains practical solutions for a Core Java course, covering various programming tasks including arithmetic operations, applet applications, string manipulations, recursion, and file handling. Each task is presented with a Java code solution, demonstrating concepts such as command line arguments, event handling, and method overloading. The document serves as a comprehensive guide for students to understand and implement Java programming techniques.

Uploaded by

shivani
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

T.Y.

BBA(CA) Sem – V

Subject : Core Java (Practical Slip Solution)

Slip No: 11 to 20

Slip 11 A) Write a menu driven java program using command line arguments for the
following:

1. Addition

2. Subtraction

3. Multiplication

4. Division.

Answer :
import [Link];
public class Slip11A {
public static void main(String args[]){
int a,b,n;
[Link]("Enter 1 : Additon" + '\n' + "Enter 2 : Substraction"
+ '\n' + "Enter 3 : Multiplication" + '\n' + "Enter 4 : Division");
DataInputStream dr = new DataInputStream([Link]);
try {
a = [Link](args[0]);
b = [Link](args[1]);
[Link]("Enter Number : ");
n = [Link]([Link]());
switch(n){
case 1:
[Link](a + " + " + b + " = " + (a+b));
break;
case 2:
[Link](a + " - " + b + " = " + (a-b));
break;
case 3:
[Link](a + " * " + b + " = " + (a*b));
break;
case 4:
[Link](a + " / " + b + " = " + (a/b));
break;
}
} catch (Exception e) {}
}
}
Slip 11 B) Write an applet application to display Table lamp. The color of lamp should
get change randomly.

Answer :

import [Link].*;

import [Link].*;

public class Slip11B extends Applet{

public float R,G,B;

Graphics gl;

public void init(){

repaint();

public void paint(Graphics g){

R = (float)[Link]();

G = (float)[Link]();

B = (float)[Link]();

Color cl = new Color(R,G,B);

[Link](0,250,290,290);

[Link](125,250,125,160);

[Link](175,250,175,160);

[Link](85,157,130,50,-65,312);

[Link](85,87,130,50,62,58);

[Link](85,177,119,89);

[Link](215,177,181,89);
[Link](cl);

[Link](78,120,40,40,63,-174);

[Link](120,96,40,40);

[Link](173,100,40,40,110,180);

/*

<applet code="[Link]" width="300" height="300">

</applet>

*/

Slip 12 A) Write a java program to display each String in reverse order from a String
array.

Answer :
class Slip12A{
public static void main(String args[]){
String arr[] = {"swarup", "Sayali", "Mahesh"};
for(int i=[Link]-1; i>=0; i--){
[Link](arr[i] + ' ');
}
}
}

Slip 12 B) Write a java program to display multiplication table of a given number into
the List box by clicking on button.

Answer :

import [Link].*;

import [Link].*;

import [Link].*;
public class Slip12B extends Applet implements ActionListener{

Button b1 = new Button("Show");

List Multi = new List();

String str ="";

public void init(){

[Link]("1");

[Link]("2");

[Link]("3");

[Link]("4");

[Link]("5");

[Link]("6");

[Link]("7");

[Link]("8");

[Link]("9");

[Link]("10");

add(Multi);

add(b1);

[Link](this);

public void paint(Graphics g){

int count = 100;

int num = [Link]([Link]());

for(int i=1;i<=10;i++){

int a = num*i;
[Link](i +" * " + i +" = "+ a,100,count);

count = count+20;

public void actionPerformed(ActionEvent e){

repaint();

/*

<applet code="[Link]" width="300" height="300">

</applet>

*/

Slip 13 A) Write a java program to accept ‘n’ integers from the user & store them in an
ArrayList collection. Display the elements of ArrayList collection in reverse order.

Answer :
import [Link].*;
class Slip13A{
public static void main(String args[]){
String temp=null;
int i,j,n;
DataInputStream dr = new DataInputStream([Link]);
try{
[Link]("Enter How May Element You Want = ");
n = [Link]([Link]());
String name[]= new String[n];
for(i=0; i<n; i++){
[Link]("Enter " + (i+1) + " String = ");
name[i] = [Link]();
}
[Link]("After Sorting = ");
for(i=n-1; i>=0; i--){
[Link](name[i] + " ");
}
}catch(Exception e){}
}
}

Slip 13 B) Write a java program that asks the user name, and then greets the user by
name. Before outputting the user's name, convert it to upper case letters. For example,
if the user's name is Raj, then the program should respond "Hello, RAJ, nice to meet
you!".

Answer :
import [Link];
class Slip13B {
public static void main(String args[]){
String str;
DataInputStream dr = new DataInputStream([Link]);
try {
[Link]("Enter Username : ");
str = [Link]();
[Link]("\"Hello, " + [Link]() + ", nice to meet
you!\"");
} catch (Exception e) {}
}
}

Slip 14 A) Write a Java program to calculate power of a number using recursion.

Answer :
import [Link].*;
class Slip14A {
public static void main(String args[]){
int base,exp;
Scanner sc =new Scanner([Link]);
[Link]("Enter the Base Number : ");
base = [Link]();
[Link]("Enter the Exponent Number : ");
exp = [Link]();
int result = power(base, exp);
[Link]("Answer : "+ result);
}
private static int power(int base, int exp) {
if(exp!=0){
return (base * power(base, exp-1));
}else{
return 1;
}
}
}

Slip 14 B) Write a java program to accept the details of employee (Eno, EName, Sal)
and display it on next frame using appropriate event .

Answer :
import [Link].*;

import [Link].*;

class Emp_details implements ActionListener {

Frame f;

Label empno, empname, sal;

TextField tempno, tempname, tsal;

Button next;

Emp_details() {

f = new Frame("\t Employee Details:");

empno = new Label("\t Employee Id:");

empname = new Label("\t Employee Name:");

sal = new Label("\t Employee Sal:");

tempno = new TextField(25);

tempname = new TextField(25);

tsal = new TextField(25);

next = new Button("Next");

[Link](empno);

[Link](tempno);

[Link](empname);

[Link](tempname);

[Link](sal);

[Link](tsal);

[Link](next);

[Link](this);

[Link](new FlowLayout());
[Link](400, 400);

[Link](true);

public void actionPerformed(ActionEvent ae) {

String empno, empname, sal;

empno = [Link]();

empname = [Link]();

sal = [Link]();

[Link](false);

new FrameDetails(empno, empname, sal);

class FrameDetails extends Frame {

Frame f;

Label empno, empname, sal;

TextField tempno, tempname, tsal;

FrameDetails(String no, String name, String s) {

f = new Frame("Employee Details:");

empno = new Label("Employee ID:");

empname = new Label("Employee Name:");

sal = new Label("Employee Salary:");

tempno = new TextField(25);

tempname = new TextField(25);

tsal = new TextField(25);


[Link](empno);

[Link](tempno);

[Link](empname);

[Link](tempname);

[Link](sal);

[Link](tsal);

[Link](no);

[Link](name);

[Link](s);

[Link](new FlowLayout());

[Link](400, 400);

[Link](true);

class Slip14B {

public static void main(String args[]) {

new Emp_details();

Slip 15 A) Write a java program to search given name into the array, if it is found then
display its index otherwise display appropriate message.

Answer :
import [Link];
class Slip15A{
public static void main(String args[]){
String arr[] = {"saurabh", "Sapkal", "Mahesh","priya"};
int i,n=0;
boolean a=false;
DataInputStream dr = new DataInputStream([Link]);
try {
[Link]("Enter String : ");
String s= [Link]();
for(i = 0; i < [Link]; i++)
{
if(arr[i].equals(s))
{
n = i;
a = true;
break;
}
}
if(a){
[Link]("arr" + "["+ i + "]");
}else{
[Link]("not Found");
}
} catch (Exception e) {}
}
}

Slip 15 B) Write an applet application to display smiley face.

Answer :
import [Link];
import [Link];
public class Slip15B extends Applet{

public void paint(Graphics g){


[Link](80, 70, 150, 150);
[Link](120, 120, 15, 15);
[Link](170, 120, 15, 15);
[Link](130, 180, 50, 20, 180, 180);
}

}
/*
<applet code="[Link]" width="300" height="300">
</applet>
*/

Slip 16 A) Write a java program to calculate sum of digits of a given number using
recursion.

Answer :
import [Link].*;
public class Slip16A {
int sum =0;
public static void main(String args[]) throws Exception{
int n;
Scanner s = new Scanner([Link]);
[Link]("Enter the Number : ");
n =[Link]();
Slip16A obj = new Slip16A();
int a = obj.sum_digit(n);
[Link]("Sum of Digit is : "+a);
}

int sum_digit(int n){


sum = n%10;
if(n==0){
return 0;
}else{
return sum +sum_digit(n/10);
}
}
}

No slip no 16 B

Slip 17 A) Write a java Program to accept ‘n’ no’s through command line and store
only armstrong no’s into the array and display that array.

Answer :
class Slip17A{
public static void main(String args[]){
int num,i,r,sum=0,temp,count=0;;
num = [Link];
int a[]= new int[num];
int b[]= new int[10];
for(i=0; i<num; i++){
a[i] = [Link](args[i]);
sum =0;
temp =a[i];
while(a[i]!=0){
r = a[i]%10;
sum = sum+r*r*r;
a[i] = a[i]/10;
}
if(temp==sum){
b[count] = temp;
count++;
}
}
for(i=0; i<count; i++){
[Link](b[i] + " ");
}
}
}

Slip 17 B) Define a class Product (pid, pname, price, qty). Write a function to accept the
product details, display it and calculate total amount. (use array of Objects)
Answer :
import [Link].*;

class Product{
String pname;
int pid, qty;
float price, total;
void accept(){
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
try {
[Link]("Enter the producat Name : ");
pname=[Link]();
[Link]("Enter pid, qty and price : ");
pid = [Link]([Link]());
qty = [Link]([Link]());
price = [Link]([Link]());
} catch (Exception e) { }
}
void display(){
total = qty*price;
[Link]("pid : " + pid + "\nProduct Nmae : "+pname+"\nQuantity :
"+qty + "\nPrice : "+price+"\n Total Amount : "+total);
}
}

class Slip17B {
public static void main(String args[]) throws IOException{
int n;
float to=0;
BufferedReader br = new BufferedReader(new InputStreamReader([Link]));
[Link]("How many Product you want to enter : ");
n = [Link]([Link]());
Product p1[]=new Product[n];
for(int i=0; i<n; i++){
p1[i]=new Product();
p1[i].accept();
}
for(int i=0; i<n; i++){
p1[i].display();
}
for(int i=0; i<n; i++){
to=to+p1[i].total;
[Link]("Total Cost : "+to);
}
}
}

Slip 18 A) Write a Java program to calculate area of Circle, Triangle & Rectangle.(Use
Method Overloading)

Answer :

import [Link].*;
class AreaCalculate{

void area(int r){

[Link]("Area of Cirlce = " + (3.14*r*r));

float area(int b, float h){

return b*h/2;

double area(Float l, Float db){

return l+db;

class Slip18A {

public static void main(String args[]){

int r, b, l, db;

float h;

Scanner br = new Scanner([Link]);

[Link]("Enter the radius, base, height, length and breadth : ");

r = [Link]();

b = [Link]();

h = [Link]();

l = [Link]();

db = [Link]();

AreaCalculate ac = new AreaCalculate();


[Link](r);

[Link]("Area of Triangle = " +[Link](b,h));

[Link]("Area of Rectange = " +[Link](l,db));

Slip 18 B) Write a java program to copy the data from one file into another file, while
copying change the case of characters in target file and replaces all digits by ‘*’ symbol.

Answer :
import [Link].*;
class Slip18B{
public static void main(String args[]) throws IOException{
FileReader fr = new FileReader("[Link]");
FileWriter fw = new FileWriter("[Link]");
int c;
while ((c=[Link]())!=-1){
if([Link](c)==false){
if([Link](c)){
[Link]([Link](c));
}else if([Link](c)){
[Link]([Link](c));
}
}else{
[Link]('*');
}
}
[Link]();
[Link]();
}
}

Slip 19 A) Write a Java program to display Fibonacci series using function.

Answer :
import [Link].*;
class Slip19A {
static void fibo() {
int i,a,b,c,n;
DataInputStream dr = new DataInputStream([Link]);
try {
[Link]("Enter Number : ");
n = [Link]([Link]());
a = b = 1;
[Link]("The Fibonacci sequence: " + a + " " + b);
for(i=1; i<=n-2; i++){
c = a + b;
[Link](" "+c);
a = b;
b = c;
}
} catch (Exception e) {}
}
public static void main(String args[]){
fibo();
}
}

Slip 20 A) Write a java program using AWT to create a Frame with title “TYBBACA”,
background color RED. If user clicks on close button then frame should close.

Answer :

import [Link].*;

import [Link].*;

class Slip20A {

public static void main(String args[]) {

JFrame frame = new JFrame("TYBBACA");

[Link](400, 400);

[Link](JFrame.EXIT_ON_CLOSE);

[Link]().setBackground([Link]);

[Link](true);

Slip 20 B) Construct a Linked List containing name: CPP, Java, Python and PHP. Then
extend your java program to do the following:

i. Display the contents of the List using an Iterator

ii. Display the contents of the List in reverse order using a List Iterator.

Answer :
import [Link].*;
public class Slip20B{
public static void main (String args[]){
LinkedList al = new LinkedList<>();
[Link]("CPP");
[Link]("JAVA");
[Link]("Python");
[Link]("PHP");
[Link]("Display content using Iterator...");
Iterator il=[Link]();
while([Link]()){
[Link]([Link]());
}
[Link]("Display Content Revverse Using ListIterator");
ListIterator li1=[Link]();
while([Link]()){
[Link]();
}
while([Link]()){
[Link]("" + [Link]());
}
}
}

Common questions

Powered by AI

The applet uses the Math.random() function to generate random RGB values, setting the color of an element by passing these values to create a new Color object. This refreshed color is then applied in the repaint cycle of the applet, displayed on each paint call .

The Java program iteratively checks each integer provided as command line arguments. It calculates the cube of each digit and sums them for comparison with the original number. If the sum matches the original number, it qualifies as an Armstrong number and is stored in an array for display .

Using AWT components, the program gathers user inputs (employee ID, name, salary) via text fields. It then hides the initial form and displays a new frame encapsulating the inputted details. This process is triggered by the ActionEvent of a button click .

The program takes input strings from the user, stores them in an ArrayList, and then iterates over the list in reverse order for display. This is achieved by initializing a loop from the last index to the first, printing each element .

The recursive method 'power' calls itself by decrementing the exponent until it reaches zero, at which point it returns 1. In each call, the base is multiplied by the result of the next recursive call, thereby building up the power computation .

During file copy operations, the program iterates through each character. Uppercase letters are converted to lowercase and vice versa. Digits are replaced with an asterisk ('*'). This transformation is conducted character by character using conditions to check character type .

A Java applet utilizes a List box for number selection and a Button to trigger the display. Upon the Button's ActionEvent, the paint method constructs and displays the multiplication table for the selected number by iterating and rendering the products sequentially on the applet canvas .

In the provided Java program, the LinkedList can be traversed using both an Iterator and a ListIterator. The Iterator allows sequential access from beginning to end, while the ListIterator also enables reverse traversal, exploiting the `hasPrevious()` method to iterate backwards .

Method overloading allows the same method name to be used for different types of parameters. In the given Java program, method overloading is used to calculate the area of a circle, triangle, and rectangle by defining distinct methods for each shape with different parameters. The method for the circle takes an integer radius, the triangle method takes an integer base and a float height, and the rectangle method takes two Float arguments for length and breadth .

The program utilizes command-line arguments to accept numerical inputs and a menu-driven approach for performing arithmetic operations. Users input arguments for the operands, while runtime input determines which operation to perform (addition, subtraction, multiplication, division) using a switch case structure .

You might also like