Java sba

1. Living and Non-Living
2. Lounge access
3. #Zipcode

Introduction:

   * Hey friends, i got to know question from some anonymous person ,here im trying to write rough code .Its  ot executed so do check by your side.

Question:

Tina and Louis play a game where one has to tell a word and a letter from that word and other has to find an occurrence of that letter(Case Sensitive)in the word and change the Case.

For instance,if the given letter present in the word is in Lowercase then the same should be converted to Uppercase and vice versa and hence display the output.


If the input string is less than 3,then display "String length of <string> is too short" and terminate the program.If the input string is greater than 10, then display "String length of <string> is too long" and terminate the program.

If the input string length is valid, then the input String should contain only alphabets , else display it as "String should not <special character/numbers> and terminate the program.

If the given character(input 2) is not present in the input string(input 1) then display it as "Character <input 2> is not found" .


Develop a java application to check the rules of the game and print the output word as given in the sample input/output.


Assumption:

  • Input string(input 1) must be one string without space.
  • Character(input 2) must be only alphabet(in Lowercase).

Note:

In the sample Input/output provided the highlighted text in bold corresponds to input provided by the user and rest of the text represents output.

Adhere to the code template if provided.


Sample Input 1:


HapPy


p


Sample output 1:


HaPpy


Explanation:-

Here the letter to be converted is p,in the given string p occurs twice as third character(p) and fourth character(P).

The third and fourth character cases have to be changed from lower to upper case and upper to lower case respectively.

Hence the output is HaPpy.


Sample Input 2:


H


Sample output 2:


String length of H is too short



Sample Input 3:


ArcHitectuRe


Sample output 3:


String length of ArcHitectuRe is too long



Sample Input 4:


HorSe


y


Sample output 4:


Character y is not found


Sample Input 5:


Shi%21


Sample output 5:


String should not contain %21



Solution:


import java.util.*;

public class Main

{

    public static void main (String[] args) throws java.lang.Exception

    {

        Scanner sc = new Scanner(System.in);

        String s = sc.nextLine();

StringBuilder str = new StringBuilder(s);

       String s1=s.toLowerCase();

 char c = sc.next().charAt(0);

Int a=s1.indexOf(c);

StringBuffer 

        num = new StringBuffer(), special = new StringBuffer(); 


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


        { 


            if (Character.isDigit(s.charAt(i))) 


                num.append(s.charAt(i)); 


            else if(Character.isAlphabetic(s.charAt(i))) 

{

/*If output not required to alpha+numeric seperately but required to orint in order having exactly as input then  remove 1st if and make this as 1st if*/

}

                


            else


                special.append(s.charAt(i)); 


        } 

Int b=num.length()+special.length();

       


    

if(b>0) {        

        System.out.println("String should not contain "+special+num); 

System.exit(0);


}

  

else if(s.length()>10) {

System.out.println("String length of "+s+" is too long");

System.exit(0);


}

else if(s.length()<3) {

System.out.println("String length of "+s+" is too short");

System.exit(0);

}

E

else if (a<0)

{

System.out.println("Character "+c+" is not found");

System.exit(0);

}


else If(a>=0)

{


        

        if(Character.isUpperCase(s.charAt(a)))  //return true if UpperCase

            str.setCharAt(a,Character.toLowerCase(s.charAt(a)));

        else

            str.setCharAt(a,Character.toUpperCase(s.charAt(a)));

       Int n2=s1.indexOf(c,a+1);/*if there are more than two occurence of passed character in testcase1 write code for n3 n4 as much as need same as code for n2 */

 If(n2>0)

{       if(Character.isUpperCase(s.charAt(n2)))  //return true if UpperCase

            str.setCharAt(n2,Character.toLowerCase(s.charAt(n2)));

        else

            str.setCharAt(n2,Character.toUpperCase(s.charAt(n2)));

  }      System.out.println(str);

}

    }

}

For doubts and explanation:





End:

Hope it will work , specify your toughts/results in comments 


Guys finally got solution from who written exam.

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; import java.io.*; import java.util.*; //all needed package imports here public class Main { //Isko theek se dekho 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 "+ m.hm.get(Cards.Amex). + " hours. Thank you!"); break; } //else // break; case Visa: if(Hm1.getKey()==a) { System.out.println("Lounge access granted for "+ m.hm.get(Cards.Visa) +" hours. Thank you!"); break; } case Masters: if(Hm1.getKey()==a) { System.out.println("Lounge access granted for "+ m.hm.get(Cards.Masters) +" hours. Thank you!"); break; } case Diners: if(Hm1.getKey()==a) { System.out.println("Lounge access denied. Thank you!"); break; } default: break; } } } }


John Samuel: Import java.io.IOException; import java.util.ArrayList; import java.util.*; //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."); } }


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"); } } }



Comments

Popular posts from this blog

Cognizant Html css js CC