Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
605 views7 pages

Dump 02

Download as txt, pdf, or txt
Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1/ 7

1.

Living and Non Living:

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:

public class Main {


//Write your code to add the numbers as per the case study using method overloading
public int addNumbers(int a, int b)
{
System.out.println("Integer Addition");
return (a+b);
}
public float addNumbers(float a, float b, float c)
{
System.out.println("Float Addition");
return (a+b+c);
}
public double addNumbers(double a, double b)
{
System.out.println("Double Addition");
return (a+b);
}
public static void main(String[] args){
Main obj = new Main();
System.out.println(obj.addNumbers(5,6));
System.out.println(obj.addNumbers(0.1f,1.2f,1.1f));
System.out.println(obj.addNumbers(1.797693,2.797693));
//call your 3 overloaded methods to check the result..
}
}

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:

public class Main {


static String string2Manipulate = " Sam woke up. Paper was on the coffee table. Sam
read the paper. Tear or shred? Paper shredded! ";
public static void main(String[] args) {
System.out.println("String before change:" + string2Manipulate);
//write your code here
String AfterTrim = string2Manipulate.trim();
String AfterReplace = AfterTrim.replaceAll("Sam", "Bheem");
String FinalValue = AfterReplace.replaceFirst("Paper", "NewsPaper");
System.out.println("String after change:" +FinalValue );
System.out.println("Last position of Paper:" +FinalValue.lastIndexOf("Paper"));
}
}

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;

//declare String array of size specified in casestudy


String[] arr = new String[5];
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter numbers to find those divisible by 10");
//Write your code to get the inputs to be added to string array. From array,
display those that are divisible by 10
for (i = 0; i < 5; i++)
{
arr[i] = br.readLine();
}
for (i = 0; i < 5; i++)
{
if (Integer.parseInt(arr[i]) % 10 == 0)
{
System.out.println(arr[i]);
divisibleBy10 = true;
}
}
// if there is no numbers added to the string array is divisible by 10, display
if (!divisibleBy10)
{
System.out.println("None divisible by 10");
}
}
}

You might also like