Dump 02
Dump 02
Dump 02
import java.io.IOException;
//import the required packages
//DO NOT Change the name of the class and its modifiers
public class Main {
//declare an array list #1
ArrayList<String> arl1 = new ArrayList<>();
static String livingThings = "Penguin";
//you can initialize living things here or add later
//DO NOT forget to add Penguin
//declare an array list #2
ArrayList<String> arl2 = new ArrayList<>();
static String nonLivingThings = "Car";
//you can initialize non-living things here or add later
//DO NOT forget to add Car
public static void main(String[] args) throws IOException {
Main m=new Main();
m.arl1.add("Dog");
m.arl1.add("Cat");
m.arl1.add("Lion");
m.arl1.add(livingThings);
m.arl2.add("Bike");
m.arl2.add("Cycle");
m.arl1.add("Specs");
m.arl2.add(nonLivingThings);
//add code to read from console
Scanner sc=new Scanner(System.in);
System.out.println("Enter a thing >>");
String thing = sc.nextLine();//complete this line
boolean foundLiving=false;
boolean foundNonLiving=false;
for(String s:m.arl1)
{
if(s.equalsIgnoreCase(thing))
foundLiving=true;
}
for(String s:m.arl2)
{
if(s.equalsIgnoreCase(thing))
foundNonLiving=true;
}
//write your code to check if the thing is available in the living things list`
if(foundLiving)
System.out.println(thing + " is a living thing!");
//write your code to check if the thing is available in the non-living things list`
if(foundNonLiving)
System.out.println(thing + " is a non-living thing!");
if (foundNonLiving==false&&foundLiving==false)
System.out.println( thing + " is *not found* in both lists.");
}
}
2.Method Overloading:
3.Palindrome:
import java.util.*;
public class Main {
public static void main(String[] args){
//declare your variables here
String input;
String finalInput=new String();
Scanner sc =new Scanner(System.in); //complete the sentence to read the user input
//HINT: one of the input streams
System.out.println("Enter words that would make up a palindrome sentence (type exit
as word if you are done):");
for(int i=0;i<=100000;i++)
{//Loop begins to accept the words from user
System.out.println("Enter your word:");
input=sc.nextLine() ;
if(input.equalsIgnoreCase("exit"))
{
break;
}
finalInput=finalInput.concat(input);//add your code here to build the sentence
//using the words provided by user
}//Loop ends to accept the words from user
String palindrome= new StringBuffer(finalInput).reverse().toString();
//add 2 lines of code to get the sentence backwards
System.out.println("Sentence forward: " + finalInput);
System.out.println("Sentence backward: " + palindrome);
if(finalInput.equalsIgnoreCase(palindrome)){
System.out.println("The words make up a palindrome sentence");
}
else
{
System.out.println("The words *DO NOT* make up a palindrome sentence");
}
sc.close();; //clean up
}
}
4.String Parsing:
5.WriteToFile:
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
// import needed package
public class Main {
//declare an array list
static ArrayList<String> proverbs = new ArrayList<String>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 3 proverbs to add");
Main m= new Main();
for(int i=0;i<3;i++)
{
m.proverbs.add(br.readLine());
}
FileWriter writer = new FileWriter("proverbs.txt");
for(int i=0;i<3;i++)
{
if(i==2)
{
writer.write(m.proverbs.get(i));
}
else
{
writer.write(m.proverbs.get(i)+ "\n");
}
}
writer.close();
//Get 3 proverbs and store in Arraylist..
//String filename =
// write into a plain text (.txt) file named proverbs.txt. Each proverb should be a
separate line.
// MUST NOT contain any empty new line as the first or last line in the text file.
System.out.println("proverbs has been added to the file!");
}
}
6.AreaOfShapes:
import java.util.Scanner;
class Shapes
{
void area()
{
System.out.println("Displaying Area of different shapes");
}
}
// Fill your code to display the formula for area of Circle and Square
class Circle extends Shapes //Make other changes but DO NOT change name of the
class
{
void area()
{
System.out.println("The area of square is pi*radius*radius");
}
}
class Square extends Shapes //Make other changes but DO NOT change name of the
class
{
void area()
{
System.out.println("The area of square is side*side");
}
}
public class Main {
public static void main(String args[])
{
//Call the method to print the formula for area of respective shapes using Runtime
Polymorphism
System.out.println("Please enter the shape for which you need the formula for
area");
Scanner sc = new Scanner(System.in);
String shape = sc.nextLine();
if (shape.equalsIgnoreCase("Circle"))
{
Shapes s = new Circle();
s.area();
}
else if(shape.equalsIgnoreCase("Square"))
{
Shapes s = new Square();
s.area();
}
}
}
7.Lounge Access:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
//all needed package imports here
public class Main {
public enum Cards { Amex, Diners, Visa, Masters }
//declare your enumerated data type for the cards here
HashMap<Cards,Integer> hm=new HashMap<Cards,Integer>();
//declare the hash map here for card type to hours allowed mapping
//Key MUST BE enumerated data type
public static void main(String[] args) throws IOException
{
//load the hash map
Main m = new Main();
m.hm.put(Cards.Amex, 4);
m.hm.put(Cards.Diners, 0);
m.hm.put(Cards.Masters, 1);
m.hm.put(Cards.Visa, 2);
//Getting the input as card type from the user..
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Welcome to TIA! Please enter the Card that you hold:");
String card = br.readLine(); //complete this statement to read the user input
//
Cards a = Cards.valueOf(card);
//case structure code goes here
for(Map.Entry Hm1:m.hm.entrySet())
{
switch (a)
{
case Amex:
if(Hm1.getKey()==a)
{
System.out.println("Lounge access granted for "+Hm1.getValue()+" hours. Thank
you!");
break;
}
//else
// break;
case Visa:
if(Hm1.getKey()==a)
{
System.out.println("Lounge access granted for "+Hm1.getValue()+" hours. Thank
you!");
break;
}
case Masters:
if(Hm1.getKey()==a)
{
System.out.println("Lounge access granted for "+Hm1.getValue()+" hours. Thank
you!");
break;
}
case Diners:
if(Hm1.getKey()==a)
{
System.out.println("Lounge access denied. Thank you!");
break;
}
default:
break;
}
}
}
}
8.ZipCode
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
//import the required packages
import java.util.HashMap;
public class Main {
//declare the Hash Map here with integer as key
static HashMap<Integer, String> hmap = new HashMap<Integer, String>();
public static void main (String[] args) throws IOException {
// load the map
//DO NOT forget to add Beverly Hills and Manhattan!!
hmap.put(90210,"Beveryly Hills");
hmap.put(10118,"Manhattan");
hmap.put(70045,"Los Angeles");
hmap.put(11111,"Kuppan Palayam");
hmap.put(10020,"Chinnaseri");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a Zip Code:");
int zipCode = Integer.parseInt(br.readLine().trim());
//your code to retrieve area for the zipCode
if(hmap.get(zipCode)!=null)
{
System.out.println("Area for " + hmap.get(zipCode));
}else{
System.out.println("Not Found");
}
}
}
9. Writetofile (Cleared)
import java.io.IOException;
// import needed package
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
imp
import java.io.FileWriter;
public class Main {
//declare an array list
static ArrayList<String> proverbs = new ArrayList<String>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter 3 proverbs to add");
//Get 3 proverbs and store in Arraylist..
for (int i = 0; i < 3; i++)
{
proverbs.add(br.readLine());
}
String filename = "proverbs.txt";
// write into a plain text (.txt) file named proverbs.txt. Each proverb should be a
separate line.
// MUST NOT contain any empty new line as the first or last line in the text file.
File file = new File("..\\" + filename);
FileWriter writer = new FileWriter(filename);
if (file.createNewFile()) {
System.out.println("File is created!");
} else {
System.out.println("File already exists., proceeding with the existing file");
}
for (String str : proverbs) {
writer.write(str + System.lineSeparator());
}
writer.close();
System.out.println("proverbs has been added to the file!");
}
}
10. Division
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException{
boolean divisibleBy10 = false;
int i;