[go: up one dir, main page]

0% found this document useful (0 votes)
270 views11 pages

Cse1007 - Java Programming LAB Digital Assignment 1 Name-Jeetesh Gowder Reg No: 19BCE2176 Slot: L21+L22

The document describes 3 Java programming assignments: 1) A program to analyze weather data over 14 days - it generates random temperatures, calculates average, and identifies hottest and coldest days. 2) A program that creates 10 Rock objects with random masses between 1-10kg, and calculates total mass. 3) A program for conference registration that stores registrations for 2 sessions in arrays, checks for duplicates between sessions, and prints results.

Uploaded by

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

Cse1007 - Java Programming LAB Digital Assignment 1 Name-Jeetesh Gowder Reg No: 19BCE2176 Slot: L21+L22

The document describes 3 Java programming assignments: 1) A program to analyze weather data over 14 days - it generates random temperatures, calculates average, and identifies hottest and coldest days. 2) A program that creates 10 Rock objects with random masses between 1-10kg, and calculates total mass. 3) A program for conference registration that stores registrations for 2 sessions in arrays, checks for duplicates between sessions, and prints results.

Uploaded by

Jeetesh Gowder
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CSE1007 – JAVA PROGRAMMING

LAB Digital Assignment 1


Name- Jeetesh Gowder
Reg No: 19BCE2176
Slot: L21+L22

Compiler used-https://www.codiva.io/
1. Use an array to solve the weather problem. Get the input from
the user for the city you live currently, the temperature for last
2 weeks in the month January 2021 (14 days).Write a java
program to find the 3 hottest days, 3 coldest day and Average
for the month. Print the values with the city details.

Algorithm-
1)Random values are selected for the temperature readings
and stored in temparray for the last 14 days by
using:
Math.random() ∗ (max − min) + min
where max = 45 C and min = 25 C as given in the
question.
2)The array temparray is then sorted into ascending
order using bubble sort.
3)The average temperature is then calculated using simple
addition and division and average is then printed.
4)Three hottest days are found and their temperatures are
printed using last three elements of temparray.
5)Three coldest days are found and their temperatures are
printed using first three elements of temparray.

Code-
import java.util.Scanner;
public class Temperature
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
double[] temparray = new double[14];
double sum = 0;
System.out.print("Enter city: ");
String myString = input.next();
System.out.println("Temperature values for last 14days:");
for(int i=0;i<14;i++)
{
temparray[i] = Math.random() * (45-25) + 25;
sum += temparray[i];
System.out.print(String.format("%.2f ",
temparray[i]));
}
for(int i=0;i<14;i++)
{
for(int j=i+1;j<14;j++)
{
if(temparray[i]>temparray[j])
{
double t=temparray[i];
temparray[i]=temparray[j];
temparray[j]=t;
}
}
}
System.out.println("\n\nCity = " + myString);
System.out.println("\n\nAverage Temperature of last 14days: " +
String.format("%.2f", sum/14));
System.out.println("\nHottest days: ");
System.out.println(String.format("%.2f ",
temparray [13]));
System.out.println(String.format("%.2f ",
temparray [12]));
System.out.println(String.format("%.2f ",
temparray [11]));
System.out.println("\nColdest days: ");
System.out.println(String.format("%.2f ",
temparray [0]));
System.out.println(String.format("%.2f ",
temparray [1]));
System.out.println(String.format("%.2f ",
temparray [2]));
}
}

OUTPUT-
2. Develop a java application that creates 10 Rock objects and
saves these in an array. Randomly select a mass between 1 and
10 kgms for each rocks as it is created. After all rocks have been
created, display their individual mass and the total mass of all
rocks.

Algorithm-

1)In the DerivedRock Class, in main function an array of


datatype Rock is made with the name rocksmass and space
of 10 elements for storing masses of 10 different rocks to
calculate their masses .
2)Using for loop each element of the rocksmass array is
initialized with an instance of the Rock Class.
3)When an object is being created for each rock, the
constructor initializes its mass with a random double
between 1 and 10 (inclusive).
4)For calculating total mass of rocks, we use variable
sum . Also we need to get the mass for each Rock. For
getting the mass of ith rock we call getMass() function
as follows:
rocksmass[i].getMass()
5)Finally, the mass of each rock is displayed by calling
display() function using instance of Rock class .
6)Also the sum of mass of all rocks is displayed in the
console.

CODE-

Code for Rock class

public class Rock


{
private double mass;
public Rock()
{
this.mass = ((Math.random()*9) + 1);
}
double getMass ()
{
return this.mass;
}
void display ()
{
System.out.println(String.format("%.2f", this.mass) + "
Kg");
}
}

Code for derivedRock class

public class derivedRock


{
public static void main(String[] args)
{
Rock[] rocksmass = new Rock[10];
double sum = 0;
for(int i = 0;i < 10;i++)
{
rocksmass[i] = new Rock();
sum += rocksmass[i].getMass();
}
String[] ar=
{"First","Second","Third","Fourth","Fifth","Sixth","Seventh","Eigh
th","Nineth","Tenth"};
for(int i = 0;i < 10;i++)
{
System.out.print("Mass of "+ar[i]+" rock = \t ");
rocksmass[i].display();
}
System.out.println("\nTotal Mass of Rocks = \t“ +
String.format(" %.2f", sum) + " Kg");
}
}

OUTPUT-
3.Write a java program for the conference registration using
string array. The number of sessions conducted is 2. Name the
sessions according to your conveyance. Get the Register no for
students for Session 1 and store it in one array. Compare with
the registrations done for Session 2 and check for duplicate
entry. No students are allowed to register for both the events,
IF so kindly throw an error message and print the same.

Algorithm-

1)Two Arrays of type String are created and named


Session1 and Session2 respectively.
2)Registration numbers for session 1 are saved in the list
Session1 and also error is thrown if there is an attempt to
register again for Session 2.
3)Now the registration numbers for session 2 are saved in
Session2 only if they do not exist in Session1 which is
checked using linear search technique in Session1 array,
Otherwise error is thrown.
Also error is thrown if there is an attempt to register again
for Session 2.
4)At the end registration numbers of both session are
displayed by printing the Session1 and Session2
Arrays.

CODE-

import java.util.*;
public class Session
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String[] Session1=new String[1000];
String[] Session2=new String[1000];
String s = "";
int a=0;
System.out.println("Session 1 registration:");
while(true)
{
System.out.print("> ");
s = sc.next().trim().toUpperCase();
if(s.equals("QUIT"))
{
break;
}
int f=0;
for(int i=0;i<a;i++)
{
if(s.equals(Session1[i]))
{
f=1;
break;
}
}
try
{
if(f==1)
{
throw new Exception("Error");
}
else
{
Session1[a]=s;
a++;
System.out.println("" + s + "
successfully registered.");
}
}
catch(Exception e)
{
System.out.println("ERROR:Already
registered");
}
}
int b=0;
System.out.println("\n\nSession 2 registration:");
while(true)
{
System.out.print("> ");
s = sc.next().trim().toUpperCase();
if(s.equals("QUIT"))
{
break;
}
int f=0;
for(int i=0;i<a;i++)
{
if(s.equals(Session1[i]))
{
f=1;
break;
}
}
int w=0;
int r=0;
try
{
if(f==1)
{
throw new Exception("Error");
}
}
catch(Exception e)
{
w=1;
System.out.println("ERROR:Already registered
in Session 1");
}
int q=0;
for(int i=0;i<b;i++)
{
if(s.equals(Session2[i]))
{
q=1;
}
}
try
{
if(q==1)
{
throw new Exception("Error");
}
}
catch(Exception e)
{
r=1;
System.out.println("ERROR:Already
registered");
}
if(w==0&&r==0)
{
Session2[b]=s;
b++;
System.out.println("" + s + " successfully
registered.");
}
}
System.out.print("Session 1: ");
for(int i=0;i<a;i++)
{
System.out.print("" + Session1[i] + " ");
}
System.out.print("\nSession 2: ");
for(int i=0;i<b;i++)
{
System.out.print("" + Session2[i] + " ");
}
sc.close();
}
}

OUTPUT-

You might also like