Question

In: Computer Science

public static GuitarType forDisplayName(String displayName) { if(displayName.equals("Electric")) return ELECTRIC; else if(displayName.equals("Acoustic")) return ACOUSTIC; else if(displayName.equals("Classical")) return...

public static GuitarType forDisplayName(String displayName) {
if(displayName.equals("Electric"))
return ELECTRIC;
else if(displayName.equals("Acoustic"))
return ACOUSTIC;
else if(displayName.equals("Classical"))
return CLASSICAL;
return null;
}

Write this if condition using for loop.

public static GuitarWood forDisplayName(String displayName) {
if(displayName.equals("Indian Rosewood"))
return INDIAN_ROSEWOOD;
else if(displayName.equals("Sitka"))
return SITKA;
else if(displayName.equals("Brazilian Rosewood"))
return BRAZILIAN_ROSEWOOD;
else if(displayName.equals("Maple"))
return MAPLE;
else if(displayName.equals("Alder"))
return ALDER;
else if(displayName.equals("Mahogany"))
return MAHOGANY;
else if(displayName.equals("Adirondack"))
return ADIRONDACK;
else if(displayName.equals("Cocobolo"))
return COCOBOLO;
else if(displayName.equals("Cedar"))
return CEDAR;
return null;
}

Write the if condition into for loop

Solutions

Expert Solution

Solution

Function implementaion as Guitartype, if condition using for loop is below:

Note: Below "length" is used for calculating the size of array .

length() used for finding length of string ,it returns the number of characters present in the string.

 /*function defintion of Guitartype take string of array type and String
    enter by user as displayName to convert upper case 
*/
        
    public static void Guitartype( String[] arr,String displayName )
    {
       for(int i=0;i<arr.length;i++)
       {
        if(arr[i].equals(displayName))  // if condition checking displayName in array
        arr[i] = arr[i].toUpperCase();  //toUpperCase use to covert into uper case 
       }
    for(int i=0;i<arr.length;i++)
        System.out.println(arr[i]); // printing of string in string array.
    }

Below is the complete code implementation of GuitarType forDisplayName as Guitartype with two parameter as one array of string type and other string type.

The given function in the program is here as Guitartype

/*save file name as upper.java */
import java.io.*;
import java.util.*;
class upper {

    //function defintion of Guitartype take string of array type and String
    // enter by user as displayName to convert upper case :
        
    public static void Guitartype( String[] arr,String displayName )
    {
       for(int i=0;i<arr.length;i++)
       {
        if(arr[i].equals(displayName))  // if condition checking displayName in array
        arr[i] = arr[i].toUpperCase();  //toUpperCase use to covert into uper case 
       }
    for(int i=0;i<arr.length;i++)
        System.out.println(arr[i]); 
    }
        public static void main (String[] args) {
                 String[] array = {"Electric", "Acoustic","Classical"};
           /*   Scanner sc= new Scanner(System.in); //System.in is a standard input stream.
        System.out.print("Enter a string: \n");
        String str= sc.nextLine();*/ 

       String str="Classical";  //here string as str taken from user 
        
        Guitartype(array,str);
        }
}

Output1.

This is the output if string enter by user is Classical and that's why from string of array type the word Classical is converted into upper case .

Second Part:

Implementaion of if using for loop as function name GuitarWood() with two parameter one is string of array and other is string.

/* function Implementation of GuitarWood display name as here is 
GuitarWood*/
/* String[] arr = {"Indian Rosewood", "Brazilian Rosewood","Maple","Alder","Mahogany",
                     "Adirondack","Cocobolo","Cedar"};
*/

public static void GuitarWood( String[] arr,String displayName )
    {
       for(int i=0;i<arr.length;i++)
       {
        if(arr[i].equals(displayName))  // if condition checking displayName in array and
        arr[i] = arr[i].toUpperCase();  //toUpperCase use to covert into uper case 
       }
    for(int i=0;i<arr.length;i++)
        System.out.println(arr[i]); 
    }

Now complete code implementation in java as below

/* GuitarWood function implementation if condition using for loop*/


import java.io.*;
import java.util.*;
class upper {

    //function defintion of Guitartype take string of array type and String
    // enter by user as displayName to convert upper case :
        
    public static void GuitarWood( String[] arr,String displayName )
    {
       for(int i=0;i<arr.length;i++)
       {
        if(arr[i].equals(displayName))  // if condition checking displayName in array
        arr[i] = arr[i].toUpperCase();  //toUpperCase use to covert into uper case 
       }
    for(int i=0;i<arr.length;i++)
        System.out.println(arr[i]); 
    }
        public static void main (String[] args) {
                 String[] array = {"Indian Rosewood", "Brazilian Rosewood","Maple","Alder","Mahogany",
                     "Adirondack","Cocobolo","Cedar"};
           /*   Scanner sc= new Scanner(System.in); //System.in is a standard input stream.
        System.out.print("Enter a string: \n");
        String str= sc.nextLine();*/ 
       String str="Maple";
        
        GuitarWood(array,str);
        }
}

In the above code array is string of array type used to store string and in java toUppercase() method used to

convert lower case into upper case.

Output.

The above image is output for string input by user is Maple and that is present in the array and that's why converted into upper case as shown in the output.


Related Solutions

public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';   ...
public static char mostFrequent(String str) {        if(str.length()==0) {            return '0';        }        String temp="";        for (int i = 0; i < str.length(); i++) {                 if(!temp.contains(String.valueOf(str.charAt(i)))) {                     temp += String.valueOf(str.charAt(i));                 }             }        char[] tempArray=stringToArray(temp);        int[] countArr=new int[tempArray.length];        int max=0;        for(int i=0;i<tempArray.length;i++) {            int cnt=numOccurences(tempArray[i],str);            countArr[i]=cnt;...
Consider this program: public class Main { public static void main(String[] args) { String s1 =...
Consider this program: public class Main { public static void main(String[] args) { String s1 = "hello"; String s2 = "hello"; String s3 = new String("hello"); System.out.println(s1 == s2); System.out.println(s2 == s3); System.out.println(s2.equals(s3)); } } When we run the program, the output is: true false true Explain why this is the output, using words and/or pictures.
public class StringTools {    public static int count(String a, char c) {          ...
public class StringTools {    public static int count(String a, char c) {           }
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { //...
Write in Java: Write a method called: public static String[] noIdenticalCombine(String[] array1, String[] array2) { // instructions: returns an array that contains all the Strings in array1 and array2 but without repetition. order does not matter, but it will return array1's elements and then array2's element that are not in array1. Assume there are no duplicates are in array1 and array2. Could use count which is how many str there are in array2, where !contains(array1, str). May an array of...
public class OOPExercises {     public static void main(String[] args) {         A objA = new...
public class OOPExercises {     public static void main(String[] args) {         A objA = new A();         B objB = new B();         System.out.println("in main(): ");         System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());         objA.setA (222);         objB.setB (333.33);       System.out.println("objA.a = "+objA.getA());         System.out.println("objB.b = "+objB.getB());     } } Output: public class A {     int a = 100;     public A() {         System.out.println("in the constructor of class A: ");         System.out.println("a = "+a);         a =...
public class GreeterTest {    public static void main(String[] args)    { // create an object...
public class GreeterTest {    public static void main(String[] args)    { // create an object for Greeter class Greeter greeter = new Greeter("Jack"); // create two variables Greeter var1 = greeter; Greeter var2 = greeter; // call the sayHello method on the first Greeter variable String res1 = var1.sayHello(); System.out.println("The first reference " + res1); // Call the setName method on the secod Grreter variable var2.setName("Mike"); String res2 = var2.sayHello(); System.out.println("The second reference " + res2);    } }...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item...
public class ArraySkills { public static void main(String[] args) { // *********************** // For each item below you must code the solution. You may not use any of the // methods found in the Arrays class or the Collections classes // You must use Java's built-in Arrays. You are welcome to use the Math and/or Random class if necessary. // You MUST put your code directly beneath the comment for each item indicated. String[] myData; // 1. Instantiate the given...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[]...
public static void main(String[] args) {        Scanner input = new Scanner(System.in);        String[] strings = new String[100];        for (int i = 0; i < strings.length; i++) {            System.out.println("Enter Strings: stop ");            strings[i]=input.nextLine();            if(strings[i].equalsIgnoreCase("stop"))                break;        }               MaxLength(strings);//here is the error    }    public static int MaxLength(String[] array) {        int max = array[0].length();       ...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts)...
import java.util.Random; class Conversions { public static void main(String[] args) { public static double quartsToGallons(double quarts) { return quarts * 0.25; } public static double milesToFeet(double miles) { return miles * 5280; } public static double milesToInches(double miles) { return miles * 63360; } public static double milesToYards(double miles) { return miles * 1760; } public static double milesToMeters(double miles) { return miles * 1609.34; } public static double milesToKilometer(double miles) { return milesToMeters(miles) / 1000.0; } public static double...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer,...
public class Main{ public static void main (String[] args) { Map<Integer, String> ssnMap = new HashMap<Integer, String>(); ssnMap.put (8675309,"Jenney"); ssnMap.put (42, "Answer to Everything"); ssnMap.put (8675309, "Stacy"); ssnMap.put (1006, "Peter"); System.out.println(ssnMap.get (8675309)); } } What is the output of the above code. Why?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT