You are on page 1of 16

BCSE103E-Computer programming java

Assignment-2

Student Name: DAGUMATI HARSHA VARDHAN

Student ID: 21BCE2879

Lab Slot: L19+L20

Date of Submission: 16/9/2022


1.Write a java program to display the counts of how many times each
word occurs in a paragraph which is given as string object. Also,
display the following:
How many characters present in the paragraph
How many words are present in the paragraph
How many unique words are present in the paragraph
identify the regions of duplicates in a given paragraph

CODE:
import java.util.Scanner;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String str = "harsha vardhan is my name";
String word;
int count1 =0;
System.out.println("Enter the word you want to find count of : ");
word = sc.nextLine();
String temp[] = str.split(" ");
int count = 0;
for (int i = 0; i < temp.length; i++) {
if (word.equals(temp[i]))
count++;
}
System.out.println("The string is: " + str);
System.out.println("The word " + word + " occurs " + count + "
times in the above string");

int count2 =0;


for (int i=0 ; i<str.length();i++)
{
if((str.charAt(i)==' ') &&
(str.charAt(i+1) != ' ') )
count++;
}

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


{
if (str.charAt(i) != ' ')
{
count1++;
}

System.out.println("The count of the characters is " +count1);


int count3 = 1;

for (int i = 0; i < str.length() - 1; i++)


{
if ((str.charAt(i) == ' ') &&
(str.charAt(i + 1) != ' '))
{
count3++;
}
}
System.out.println("Number of words in a string : " + count3);

boolean[] arr = new boolean[temp.length];


int j, i = 0;
int count4 = 0;
for (i = 0; i < temp.length; i++) {
if (!arr[i]) {
count4++;
for (j = i + 1; j < temp.length; j++) {
if (temp[j].compareTo(temp[i]) == 0) {
arr[j] = true;
count4--;
}}}}
}}
CODE SCREENSHOT:
Output:

2. Write a java program to get every hour temperature from 6 AM


to 12 PM every day (totally 5 days and 6 temperature readings
per day) for the month of August , 2022. Then find out the
following:

•3 hottest days along with the time at which the hottest


temperature reading is recorded

•3 coldest days along with the time at which the hottest


temperature reading is recorded

•3 averagely hottest days along with the time at which the


hottest temperature reading is recorded

Code:
import java.util.*;

public class Main

public static void main(String[] args) {

Scanner sc=new Scanner(System.in);

int[][] a=new int[5][6];

System.out.println("Enter the temperatures of August


month");

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

System.out.println("Enter" + " DAY "+(i+1 ) +" "+


"temperatures");

System.out.println(" ");

for(int j=0;j<6;j++){

System.out.println("Enter" +" " +(j+6)+ "AM


temperatures");

a[i][j]=sc.nextInt();

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

for(int j=0;j<6;j++){

System.out.print(a[i][j]+" ");

}
System.out.println("");

int temp[] = new int[5];

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

int max =0;

for (int j=0;j<6;j++)

if (a[i][j]>max)

max = a[i][j];

temp[i] = max;

i++;

Arrays.sort(temp);
for (int i = 0; i < 5; i++)

for ( int j = 0; j < 6; j++)

if(a[i][j]==temp[5-1])

System.out.println("First hottest day is in DAY


"+(i+1)+" and "+(j+6)+"AM");

else if(a[i][j]==temp[5-2])

System.out.println("Second hottest day is in DAY


"+(i+1)+" and "+(j+6)+"AM");

else if(a[i][j]==temp[5-3])

System.out.println("Third hottest day is in DAY


"+(i+1)+" and "+(j+6)+"AM");

int temp1[] = new int[5];


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

int min =Integer.MAX_VALUE;

for (int j=0;j<6;j++)

if (a[i][j]<min)

min = a[i][j];

temp1[i] = min;

i++;

Arrays.sort(temp1);

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

for (int j = 0; j < 6; j++)

if(a[i][j]==temp1[0])

{
System.out.println("First coldest day is in DAY
"+(i+1)+" and "+(j+6)+"AM");

else if(a[i][j]==temp1[1])

System.out.println("Second coldest day is in DAY


"+(i+1)+" and "+(j+6)+"AM");

else if(a[i][j]==temp1[2])

System.out.println("Third coldest day is in DAY


"+(i+1)+" and "+(j+6)+"AM");

float avg[] = new float[5];

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

int sum =0;

for(int j=0; j<6 ; j++)


{

sum += a[i][j];

avg[i] = sum/6;

//Arrays.sort(avg);

float b[][] = new float[5][1];

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

for (int j=0 ; j<1 ; j++)

b[i][j] = avg[i];

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

{
for (int j =0 ; j< 1 ; j++)

System.out.println(b[i][j]);

Arrays.sort(avg);

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

for (int j =0 ; j<1 ; j++)

if (b[i][j]==avg[5-1])

System.out.println("First Avg hottest


temperature is in DAY "+(i+1));

else if (b[i][j]==avg[5-2])

{
System.out.println("Second Avg hottest
temperature is in DAY "+(i+1));

else if(b[i][j]==avg[5-3])

System.out.println("third Avg hottest


temperature is in DAY "+(i+1));

Code screenshot
Code output:

You might also like