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

Java Week - 13

Java code
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)
27 views11 pages

Java Week - 13

Java code
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
You are on page 1/ 11

Page no:

Date:……….

WEEK-13
1) Develop the logic for the following scenario. Consider the class Cuboid with length, breadth,
and height as private attributes. Code the parameterized constructor and toString() method.
Develop main() method that stores data of Cuboids as a collection and must be menu driven with
the following operations: 1. Add new Cuboid, 2) Sort by length, 3) Sort by Volume.
[HINT:Volume of cuboid is length*breadth*height].

Class Diagram:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

Program:
package week13;

public class Cuboid {


private double length;
private double breadth;
private double height;
public Cuboid(double length,double breadth,double height) {
this.length=length;
this.breadth=breadth;
this.height=height;
}
public double getLength() {
return length;
}
public void setLength(double length) {
this.length = length;
}
public double getBreadth() {
return breadth;
}
public void setBreadth(double breadth) {
this.breadth = breadth;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double volume() {
return length*breadth*height;
}
@Override
public String toString() {
return "Cuboid [length=" + length + ", breadth=" + breadth +
", height=" + height + ", volume()="
+ volume() + "]";
}

}
package week13;

import java.util.Comparator;

public class sortbyLength implements Comparator<Cuboid> {

@Override
public int compare(Cuboid o1, Cuboid o2) {

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

return (int)(o1.getLength()-o2.getLength());
}

}
package week13;

import java.util.Comparator;

public class sortByVolume implements Comparator<Cuboid> {

@Override
public int compare(Cuboid o1, Cuboid o2) {
return (int)(o1.volume()-o2.volume());
}

}
package week13;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;

public class Demo {


public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
ArrayList<Cuboid> list = new ArrayList<Cuboid>();
while(true) {
System.out.println("******MENU******");
System.out.println("1.Add new Cuboid");
System.out.println("2.Dispaly");
System.out.println("3.Sort by length");
System.out.println("4.Sort by volume");
System.out.println("5.Terminate");
System.out.println("Enter your choice");
int choice=sc.nextInt();
switch(choice) {
case 1:
list.add(new
Cuboid(sc.nextDouble(),sc.nextDouble(),sc.nextDouble()));
break;
case 2:
for(Cuboid c: list) {
System.out.println(c);
}
break;
case 3:
Collections.sort(list,new sortbyLength());
for(Cuboid c: list) {
System.out.println(c);
}
break;

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

case 4:
Collections.sort(list,new sortByVolume());
for(Cuboid c: list) {
System.out.println(c);
}
break;
case 5:
System.exit(1);
break;
default:
System.out.println("Enter a valid choice");
}
}

}
}
Output:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

2) Write a program that prompts the user to enter a text file name and displays the number of
vowels and consonants in the file. Use a set to store the vowels A, E, I, O, and U(Hint: Use
HashSet).

Class Diagram:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

Program:
package lab13_1;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;

public class Question_2 {


public static void main(String args[]) throws
FileNotFoundException {
File fout= new File("C:\\Users\\vamsi\\eclipse-workspace\\
JAVA project\\src\\lab13_1\\hello");
Scanner input = new Scanner(fout);
HashSet<Character> c = new HashSet<>();
c.add('A');
c.add('E');
c.add('I');
c.add('O');
c.add('U'); int vowels=0,consonants=0;
while(input.hasNext()) {
String s= input.nextLine();
s=s.toUpperCase();
for(int i=0;i<s.length();i++) {
if(c.contains(s.charAt(i))) {
vowels++;
}
else {
if(Character.isAlphabetic(s.charAt(i))) {
consonants++;
}
}

}
}
System.out.println("Vowel count: "+vowels);
System.out.println("Consonant count: "+consonants);
input.close();
}
}

Output:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

3) Write a program that reads an unspecified number of integers and finds the one that has the
most occurrences. The input ends when the input is 0. For example, if you entered 2 3 40 3 5 4 –
3 3 3 2 0, the number 3 occurred most often. If not one but several numbers have the most
occurrences, all of them should be reported. For example, since 9 and 3 appear twice in the list 9
30 3 9 3 2 4, both occurrences should be reported.(Hint:Use HashMap).

Class Diagram:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

Program:

package lab13_1;

import java.util.Collections;

import java.util.HashMap;

import java.util.HashSet;

import java.util.Scanner;

public class Question_3 {

public static void main(String args[]) {

Scanner sc= new Scanner(System.in);

HashMap<Integer,Integer> map = new HashMap<>();

System.out.println("Enter the values");

while(true) {

int key= sc.nextInt();

if(key==0)

break;

if(map.containsKey(key)) {

int value=map.get(key);

value++;

map.put(key, value);

else

map.put(key, 1);

int max=Collections.max(map.values());

HashSet<Integer> set = new HashSet<>();

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY
Page no:
Date:……….

set.addAll(map.keySet());

System.out.println("The elements that occured the maximum times are :");

for(Integer i: set) {

if(map.get(i)== max)

System.out.print(i+" ");

sc.close();

Output:

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING


PRASAD V POTLURI SIDDARTHA INSTITUE OF TECHNOLOGY

You might also like