0% found this document useful (0 votes)
27 views

Collection and Generics

Java Programs

Uploaded by

sapnadoharey3005
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 views

Collection and Generics

Java Programs

Uploaded by

sapnadoharey3005
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/ 8

Babithas app

import java.util.TreeMap;

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the paragraph typed");

String sentence = sc.nextLine();

TreeMap<String, Integer> wordFrequency = countWords(sentence);

displayWordFrequency(wordFrequency);

public static TreeMap<String, Integer> countWords(String sentence) {

TreeMap<String, Integer> wordFrequency = new TreeMap<>();

int totalWordCount = 0;

sentence = sentence.toLowerCase();

String[] words = sentence.split("\\s+");

for (String word : words) {

word = removeSpecialCharacters(word);

if (!word.isEmpty()) {

wordFrequency.put(word, wordFrequency.getOrDefault(word, 0) + 1);

totalWordCount++;

}
}

wordFrequency.put("TotalWords", totalWordCount);

return wordFrequency;

public static String removeSpecialCharacters(String word) {

word = word.replace(",", "");

word = word.replace(";", "");

word = word.replace(":", "");

word = word.replace(".", "");

word = word.replace("!", "");

word = word.replace("?", "");

word = word.replace("@", "");

word = word.replace("#", "");

word = word.replace("$", "");

word = word.replace("%", "");

return word;

public static void displayWordFrequency(TreeMap<String, Integer> wordFrequency) {

System.out.println("Total number of words "+wordFrequency.get("TotalWords"));

wordFrequency.remove("TotalWords");

System.out.println("Words with count");

for (String word : wordFrequency.keySet()) {

int count = wordFrequency.get(word);

System.out.println(word + " - " + count);

}
STOCK LIST

import java.util.ArrayList;

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

System.out.println("Enter the number of stocks to add");

int noOfStocks = sc.nextInt();

sc.nextLine();

ArrayList<String> list = new ArrayList<>();

while(noOfStocks-->0){

String stock = sc.nextLine();

list.add(stock);

System.out.println(list);

}
Plip event

Student Utility class

import java.util.Map;

import java.util.HashMap;

public class StudentUtility{

private Map<String, Double> studentMap;

public StudentUtility(){

this.studentMap = new HashMap<String,Double>();

public Map<String,Double> getStudentMap(){

return studentMap;

public void setStudentMap(Map<String,Double> studentMap){

this.studentMap = studentMap;

public void addStudentDetails(String studentName, Double score){

this.studentMap.put(studentName,score);

public int filterStudentDetails(){

int count = 0;

for(Map.Entry<String,Double> entry:studentMap.entrySet()){

Double score = entry.getValue();

if(score > 90) count++;


}

return count;

Main function

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("Enter the number of students");

int noOfStudents = sc.nextInt();

StudentUtility stuUtil = new StudentUtility();

while(noOfStudents-->0){

System.out.println("Enter the student name");

String name = sc.next();

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

double score = sc.nextInt();

stuUtil.addStudentDetails(name,score);

int count = stuUtil.filterStudentDetails();


if(count == 0){

System.out.println("No students found");

}else{

System.out.println("Count is "+count);

Top Tier Motors

Main function

import java.util.Scanner;

public class Main

public static void main(String[] args) {

Scanner sc= new Scanner(System.in);

System.out.println("Enter the number of vehicles");

int noOfVehicles = sc.nextInt();

sc.nextLine();

VehicleUtility vehUtil = new VehicleUtility();

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

System.out.println("Enter the vehicle name and price of Vehicle "+(i+1));

String name = sc.nextLine();

double price = sc.nextDouble();

sc.nextLine();

vehUtil.addVehiclePriceDetails(name,price);
}

while(true){

System.out.println("Enter the vehicle name to be searched");

String name = sc.nextLine();

double discountedPrice = vehUtil.calculateCostAfterDiscount(name);

if(discountedPrice == -1){

System.out.println(name+" is not available currently");

return;

System.out.println("Price after discount "+name+" is "+discountedPrice);

System.out.println("Do you want to continue (Y/N)");

String choice = sc.next();

sc.nextLine();

if(choice.equals("N")){

System.out.println("Thank you for using the Application");

return;

Vehicle utility class

import java.util.HashMap;

import java.util.Map;

public class VehicleUtility{


private Map<String,Double> vehicleMap = new HashMap<String,Double>();

public Map<String,Double> getVehicleMap(){

return vehicleMap;

public void setVehicleMap(Map<String,Double> vehicleMap){

this.vehicleMap = vehicleMap;

public void addVehiclePriceDetails(String vehicleName, double price){

this.vehicleMap.put(vehicleName,price);

public double calculateCostAfterDiscount(String vehicleName){

String givenVehicleType = vehicleName.split(" ")[0];

double discountValue = 0;

if(givenVehicleType.equals("TVS")) discountValue = 10;

else if(givenVehicleType.equals("Honda")) discountValue = 5;

else discountValue = 7;

if(!this.vehicleMap.containsKey(vehicleName)) return -1;

double vehiclePrice = this.vehicleMap.get(vehicleName);

return (vehiclePrice)-(discountValue*vehiclePrice/100.0);

You might also like