You are on page 1of 9

JAVA LAB ASSIGNMENT-1

M.BHANU

21BCE3879

21B
CE
Q1

387
9
CODE:

import java.util.*;

import java.math.*;

public class Q1 {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

String rstName, lastName;

oat height, weight;

System.out.println("Enter your rst name: ");

rstName = input.next();

System.out.println("Enter your last name: ");

lastName = input.next();

System.out.println("Enter your height(in meters): ");

height = input.nextFloat();

System.out.println("Enter your weight(in kilograms): ");

weight = input.nextFloat();

double h2 = Math.pow(height, 2);

double bmi = weight/h2;

System.out.print( rstName + " " + lastName + ", ");

if(bmi<18.5)

System.out.println("You are in the category of Underweight");

else if (bmi >= 18.5 && bmi < 25)

System.out.println("You are in the category of Normal(healthy weight)");

fl
fi
fi
fi
fi
else if(bmi >= 25 && bmi <30)

System.out.println("You are in the category of Overweight");

else if(bmi >= 30)

System.out.println("You are in the category of Obese class");

else

System.out.println("Please enter valid details");

OUTPUT:

21B
CE
387
9
Q2

CODE:

import java.util.*;

public class Q2 {

public static void main(String[] args) {

int[][] employeeHours =

{{2 ,4, 3, 4, 5, 8, 8},

{7, 3, 4, 3, 3, 4, 4},

{3, 3, 4, 3, 3, 2, 2},

{9, 3, 4, 7, 3, 4, 1},

{3, 5, 4, 3, 6, 3, 8}};

//int[][] totalHoursPerEmployee = new int[7][1];

int[] totalHoursPerEmployee = sumEmployeeHours(employeeHours);

int[] indexList = new int[totalHoursPerEmployee.length];

sortWhileKeepingIndices(totalHoursPerEmployee, indexList);

21B
for (int i = 0; i < totalHoursPerEmployee.length; i++) {

System.out.printf("Employee %s worked %d hours%n", indexList[i],

CE
totalHoursPerEmployee[i]);

387
System.out.println(java.util.Arrays.toString(indexList));

9
}

public static int[] sumEmployeeHours(int[][] hours) {

int[] totalHoursPerEmployee = new int[hours.length];

for (int i = 0; i < hours.length; i++) {

int sum = 0;

for (int j = 0; j < hours[i].length; j++) {

sum += hours[i][j];

totalHoursPerEmployee[i] = sum;

return totalHoursPerEmployee;

static void sortWhileKeepingIndices(int[] totalHours, int[] indexList) {

// ll indexList

for (int i = 0; i < indexList.length; i++) {

indexList[i] = i;

for (int i = 0; i < totalHours.length; i++) {

int currentMax = totalHours[i];

int currentMaxIndex = i;

for (int j = i + 1; j < totalHours.length; j++) {

if (currentMax < totalHours[j]) {

currentMax = totalHours[j];

fi
currentMaxIndex = j;

if (currentMaxIndex != i) {

totalHours[currentMaxIndex] = totalHours[i];

totalHours[i] = currentMax;

int temp = indexList[currentMaxIndex];

indexList[currentMaxIndex] = indexList[i];

indexList[i] = temp;

System.out.println(java.util.Arrays.toString(indexList) + " - " +

21B
java.util.Arrays.toString(totalHours));

CE
OUTPUT:

387
9

Q3

3. Develop a Java Program to count the number of votes voted for each candidate. Consider there
are three candidates represented by the digit (1,2,3) and 10 voters have to vote. The voting process
is simulated and automated through a random number generator method that will randomly select
the digit from 1-to 3 and vote for him. Note: Math.random() * (max - min + 1) + min

CODE:
import java.util.Scanner;
public class test
{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
nal int SIZE = 3;
String[] votes = new String[10];
String[] names = new String[SIZE];
int count1=0;

21B
int count2=0;
int count3=0;

CE
for (int i = 0; i < names.length; i++){
System.out.print("Enter candidate's name: ");
names[i] = input.next( );

387
}
for(int i = 0; i < votes.length; i++){
System.out.print("Enter your vote: ");

9
votes[i] = input.next( );
if(votes[i]==names[0]){
count1++;
}
else if(votes[i]==names[1]){
count2++;
}
else{
count3++;
}
}

System.out.println("votes for 1 is: " +count1);


System.out.println("votes for 2 is: " +count2);
System.out.println("votes for 3 is: " +count3);
}}

OUTPUT

Q4

fi
21B
CE
387
4. Write a program to demonstrate the knowledge of students in advanced concepts of Java string
handling. Eg., (Bioinformatics: nding genes) Biologists use a sequence of letters A, C, T, and G to
model a genome. A gene is a substring of a genome that starts after a triplet ATG and ends before a

9
triplet TAG, TAA, or TGA. Furthermore, the length of a gene string is a multiple of 3 and the gene
does not contain any of the triplets ATG, TAG, TAA, and TGA. Write a program that prompts the
user to enter a genome and displays all genes in the genome. If no gene is found in the input
sequence, displays no gene. Here are the sample runs: Enter a genome string:
TTATGTTTTAAGGATGGGGCGTTAGTT O/P: TTT GGGCGT

CODE:

import java.util.*;

public class Q4 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter a genome string : ");

String str = sc.nextLine();

int len = str.length();

System.out.println("Genes in the genome are :");

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

for(int j=i+1;j<=len;j++) {

String s = str.substring(i,j);

if(s.length()>=9 && s.substring(0,3).equals("ATG")) {

int t = s.length();

if(s.substring(t-3).equals("TAG") || s.substring(t-3).equals("TAA") ||
s.substring(t-3).equals("TGA")) {

String temp = s.substring(3,t-3);

if(temp.length()%3==0) {

System.out.println(temp);

fi
}

OUTPUT:

21B
CE
387
9

Q5

5. Write a program to demonstrate the knowledge of students in working with classes and objects.
Eg.,Create a class Film with string objects which stores name, language and lead_actor and
category (action/drama/ ction/comedy). Also include an integer data member that stores the
duration of the lm. Include parameterized constructor, default constructor and accessory functions
to lm class. Flim objects can be initialized either using a constructor or accessor functions. Create
a class FilmMain that includes a main function. In the main function create a vector object that
stores the information about the lm as objects. Use the suitable methods of vector class to iterate
the vector object to display the following
a. The English lm(s) that has Arnold as its lead actor and that runs for shortest duration.
b. b. The Tamil lm(s) with Rajini as lead actor.

CODE:

import java.util.*;

fi
fi
fi
fi
fi
fi
class Film{

String name;

String language;

String lead_actor;

String category;

int duration;

Film(){

name = null;

language = null;

lead_actor = null;

category = null;

duration = 0;

21B
}

Film(String n, String lang, String actor, String cat, int dur){

CE
name = n;

language = lang;

387
lead_actor = actor;

category = cat;

duration = dur;

9
}

public class Q5 {

public static void main(String[] args) {

Film lms;

Vector<Film> Films = new Vector<Film>();

lms = new Film();

lms.name = "Terminator";

lms.language = "English";

lms.lead_actor = "Arnold";

lms.duration = 10;

lms.category = "Action";

Films.add( lms);

lms = new Film();

lms.name = "Terminator 2";

lms.language = "English";

lms.lead_actor = "Arnold";

lms.duration = 20;

lms.category = "Action";

Films.add( lms);

lms = new Film();

lms.name = "Robot 1";

lms.language = "Tamil";

lms.lead_actor = "Rajini";

lms.duration = 10;

fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
lms.category = "Comedy";

Films.add( lms);

lms = new Film();

lms.name = "Robot 2";

lms.language = "Tamil";

lms.lead_actor = "Rajini";

lms.duration = 30;

lms.category = "Comedy";

Films.add( lms);

int min = 9999999;

int imin = 0;

21B
for(int i = 0; i < 4; i++){

lms = Films.get(i);

if( lms.lead_actor.equals("Arnold"))

CE
if( lms.duration < min){

min = lms.duration;

387
imin = i;

if( lms.category.equals("Comedy"))

9
System.out.println( lms.name + " is a Comedy movie");

lms = Films.get(imin);

System.out.println("Shortest Arnold lm is " + lms.name + " and duration: " + min);

OUTPUT:

fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi

You might also like