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

Java Lab Programs for Beginners

The document contains a series of Java lab programs that cover various topics including generating prime numbers, solving quadratic equations, printing Fibonacci numbers, creating a pyramid shape, implementing a bank account system, analyzing temperature data, counting election votes, merging sorted arrays, and calculating cube properties. Each program includes code snippets demonstrating the implementation of the described functionality. The document serves as a practical guide for learning Java programming through hands-on exercises.

Uploaded by

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

Java Lab Programs for Beginners

The document contains a series of Java lab programs that cover various topics including generating prime numbers, solving quadratic equations, printing Fibonacci numbers, creating a pyramid shape, implementing a bank account system, analyzing temperature data, counting election votes, merging sorted arrays, and calculating cube properties. Each program includes code snippets demonstrating the implementation of the described functionality. The document serves as a practical guide for learning Java programming through hands-on exercises.

Uploaded by

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

JAVA : LAB PROGRAMS

1 .
a) Generate prime numbers for the given range.
// Generate prime numbers for the given range.
import [Link].*;
import [Link];
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
int n1, n2, i, j, flag;
[Link]("Enter the lower no. of interval : ");
n1 = [Link]();
[Link]("Enter the higher no. of interval : ");
n2 = [Link]();
[Link]("Prime no. between "+n1+" and "+n2+" are : ");
for(i=n1;i<=n2;i++){
if(i==1 || i==0)
continue;
flag = 1;
for(j=2;j<=i/2;j++){
if(i%j == 0){
flag = 0;
break;
}
}
if(flag == 1)
[Link](i+" ");
}
}
}

b) Find all the roots of a quadratic equation.


// Find all the roots of a quadratic equation
import [Link].*;
import [Link];
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
double a, b, c, det, r1, r2;
[Link]("Enter non-zero co-effecient value : ");
a = [Link]();
b = [Link]();
c = [Link]();
det = b*b - 4*a*c;
if(det > 0){
[Link]("Roots are real and distinct ");
r1 = (-b + [Link](det))/(2*a);
r2 = (-b - [Link](det))/(2*a);
[Link]("Root-1 : "+r1+" and Root-2 : "+r2);
}
else if(det == 0){
[Link]("Roots are real and equal ");
r1 = r2 = (-b)/(2*a);
[Link]("Root-1 = Root-2 = "+r1);
}
else{
[Link]("Roots are complex and imaginary");
double real = (-b)/(2*a);
double imaginary = [Link](-det)/(2*a);
[Link]("Root-1 = "+real+" + "+imaginary+"i");
[Link]("Root-2 = "+real+" - "+imaginary+"i");
}
}
}
c) Print ‘N’ Fibonacci numbers.
// Print ‘N’ Fibonacci numbers.
import [Link].*;
import [Link];
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
int N, n1=0, n2=1, n3, i;
[Link]("Enter the value of N : ");
N = [Link]();
[Link]("Fibonacci sequence upto "+N+" terms : "+n1+" "+n2);
for(i=2;i<N;i++){
n3 = n1+n2;
[Link](" "+n3);
n1 = n2;
n2 = n3;
}
}
}
d) Print pyramid shape using * symbols.
// Print pyramid shape using * symbols.
import [Link].*;
import [Link];
public class Main{
public static void print(int n){
for(int i=0;i<n;i++){
for(int j=n-i;j>1;j--)
[Link](" ");
for(int j=0;j<=i;j++)
[Link]("* ");
[Link]();
}
}

public static void main(String args[]){


Scanner in = new Scanner([Link]);
int a;
[Link]("Enter no. of * required at base : ");
a = [Link]();
print(a);
}
}

2 . Define a class to represent a bank ACCOUNT and include the following members:
I. Data Members(States):
a) Name of Depositor
b) Account number
c) Type of Account
d) Balance amount in the account
II. Member Methods(Behaviors):
a) To assign initial values
b) To deposit an amount
c) To withdraw an amount after checking for the balance
d) To display name & balance
III. Define EXECUTEACCOUNT class that defines main method to test above class.
IV. In the above class, maintain the total number of account holders present in
the bank and also define a method to define it. Change the main method
appropriately.
V. In main method of EXECUTEACCOUNT class, define an array to handle five
accounts.
VI. In ACCOUNT class constructor, demonstrate the use of “this” keyword

// Bank function
import [Link].*;
import [Link];
class Bankdetails{
String name;
String acc_type;
String acc_no;
int balance;
Scanner in = new Scanner([Link]);
void openaccount(){
[Link]("Enter name : ");
name = [Link]();
[Link]("Enter account no. : ");
acc_no = [Link]();
[Link]("Enter account type : ");
acc_type = [Link]();
[Link]("Enter balance : ");
balance = [Link]();
}
void display(){
[Link]("Name : "+name);
[Link]("Account type : "+acc_type);
[Link]("Account no. : "+acc_no);
[Link]("Balance : "+balance);
}
void deposit(){
[Link]("Enter amount to deposit : ");
int amt = [Link]();
balance += amt;
}
void withdraw(){
[Link]("Enter ammount to withdraw : ");
int amt = [Link]();
if(balance >= amt){
balance -= amt;
[Link]("Balance after withdrawing "+amt+" = "+balance);
}
else{
[Link]("Transaction failed!!! Balance is less than amount
requested");
}
}
public boolean search(String an){
if(acc_no.equals(an)){
display();
return true;
}
return false;
}
}
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
[Link]("Enter no. of customers : ");
int n = [Link]();
Bankdetails[] c = new Bankdetails[n];
for(int i=0; i<[Link]; i++){
c[i] = new Bankdetails();
c[i].openaccount();
}
int ch;
do{
[Link]("Enter your choice : ");
[Link]("1. Display \n2. Search account by account no.\n3.
Deposit\n4. Withdraw\n0. Exit\n");
ch = [Link]();
switch(ch){
case 1 : for(int i=0;i<[Link];i++){
c[i].display();
}
break;
case 2 : [Link]("Enter account no. to search : ");
String an = [Link]();
boolean found = false;
for(int i=0;i<[Link];i++){
found = c[i].search(an);
if(found){
break;
}
}
if(!found)
[Link]("Search failed");
break;
case 3 : [Link]("Enter account no. to deposit : ");
an = [Link]();
found = false;
for(int i=0;i<[Link];i++){
found = c[i].search(an);
if(found){
c[i].deposit();
break;
}
}
if(!found)
[Link]("Search failed");
break;
case 4 : [Link]("Enter accoutn no. to withdraw : ");
an = [Link]();
found = false;
for(int i=0;i<[Link];i++){
found = c[i].search(an);
if(found){
c[i].withdraw();
}
}
if(!found)
[Link]("Search failed");
break;
case 0 : [Link]("--------Thankyou-----");
break;
default : [Link]("Invalid choice");
break;
}
}while(ch != 0);
}
}
3
The daily maximum temperatures recorded for 5 cities during the first 6 days of
January month have to be tabulated. Develop an application to read the
data and find the city and day corresponding to highest temperature and lowest
temperature.
// Java program to calculate max temp and min temp in 6days of Jan
import [Link].*;
import [Link];
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
City[] city = new City[6];
for(int i=0;i<6;i++)
city[i] = new City();
for(int i=0;i<6;i++){
[Link]("Enter details of city-"+(i+1)+" : ");
city[i].read();
city[i].cal();
}
int ch=0;
while(ch != 2){
[Link]("Press 1 to display\nPress 2 to exit");
ch = [Link]();
if(ch ==1){
[Link]("Enter the city no. : ");
int ctn = [Link]();
if(ctn > 5 || ctn < 1)
[Link]("Enter valid no. ");
else
city[ctn-1].display();
}
else if(ch ==2)
break;
else
[Link]("Enter correct option");
}
[Link]();
}
}
class City{
Scanner in = new Scanner([Link]);
int[] t = new int[6];
int high, low, hd, ld;
String name;
public void read()
{
[Link]("Enter the name of city : ");
name = [Link]();
for(int i=0;i<6;i++){
[Link]("Enter the temperature on Jan "+(i+1)+" : ");
t[i] = [Link]();
}
}
public void cal()
{
high = t[1];
low = t[1];
for(int i=0; i<6; i++){
if(t[i] > high){
high = t[i];
hd = i+1;
}
if(t[i] < low){
low = t[i];
ld = i+1;
}
}
}
public void display(){
[Link]("City Name : "+name);
[Link]("Highest temperataure is "+high+" on Jan "+hd);
[Link]("Lowest temperataure is "+low+" on Jan "+ld);
}
}
4
An election is contested by 5 candidates. The candidates are numbered 1 to 5 and
the voting is done by marking the candidate number on the ballot paper. Develop an
application to read the ballots and count the votes cast for each candidate using
an array variable count. In case, a number read is outside the range 1 to 5, the
ballot should be considered as a 'spoilt ballot' and the program should also count
the number of spoilt ballots

// Program to read ballot no. and to count no. of votes and spoilt votes
import [Link].*;
import [Link];
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
int[] c = {0,0,0,0,0,0};
int spoilt=0;
int ch;
[Link]("1. Candidate-1\n2. Candidate-2\n3. Candidate-3\n4.
Candidate-4\n5. Candidate-5 \n0. To get result");
do{
[Link]("Enter no. to vote candidate : ");
ch = [Link]();
switch(ch){
case 1 : c[0]++; break;
case 2 : c[1]++; break;
case 3 : c[2]++; break;
case 4 : c[3]++; break;
case 5 : c[4]++; break;
default : spoilt++; break;
}
}while(ch != 0);
[Link]("----RESULT-------");
[Link]("Candidate-1 : "+c[0]+" votes\nCandidate-2 : "+c[1]+"
votes\nCandidate-3 : "+c[2]+" votes\nCandidate-4 : "+c[3]+" votes\nCandidate-5 :
"+c[4]+" votes\nSpoilt : "+(spoilt-1)+" votes");
}
}

5 . Given are two one dimensional array A and B which are sorted in ascending
order. Develop an application to merge them into a single sorted array C that
contains every item form A and B, in ascending order.

// Java program to merge two sorted arrays


import [Link].*;

class Main
{
public static void mergearrays(int[] A, int[] B, int n1,
int n2, int[] C)
{
int i = 0, j = 0, k = 0;
while (i<n1 && j <n2)
{
if (A[i] < B[j])
C[k++] = A[i++];
else
C[k++] = B[j++];
}
while (i < n1)
C[k++] = A[i++];
while (j < n2)
C[k++] = B[j++];
}

public static void main (String args[])


{
int i;
Scanner in = new Scanner([Link]);
int[] A = new int[10];
[Link]("Enter no. of elements of array - A : ");
int n1 = [Link]();
[Link]("Enter "+n1+" elements into array - A : ");
for(i=0;i<n1;i++){
A[i] = [Link]();
}
int[] B = new int[10];
[Link]("Enter no. of elements of array - B : ");
int n2 = [Link]();
[Link]("Enter "+n2+" elements into array - B : ");
for(i=0;i<n2;i++){
B[i] = [Link]();
}

int[] C = new int[n1+n2];


mergearrays(A, B, n1, n2, C);

[Link]("Array after merging");


for (i=0; i < n1+n2; i++)
[Link](C[i] + " ");
}
}

6.
import [Link].*;
import [Link];
class CUBE{
Scanner in = new Scanner([Link]);
int vol, out_ar, j=1, k=1;
int[] a = new int[20];
void read(int n){
for(int i=0; i<n; i++){
[Link]("Enter the length of side of cube-"+j+" : ");
a[i] = [Link]();
j++;
}
}
void calculate(int n){
for(int i=0; i<n;i++){
vol = a[i]*a[i]*a[i];
out_ar = 6*a[i]*a[i];
[Link]("Vole=ume of cube-"+k+" = "+vol);
[Link]("Outer area of cube-"+k+" = "+out_ar);
k++;
}
}
}
public class Main{
public static void main(String args[]){
Scanner in = new Scanner([Link]);
int n;
[Link]("Enter no. of cubes : ");
n = [Link]();
CUBE cube = new CUBE();
[Link](n);
[Link](n);
}
}

You might also like