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 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
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.