A. Write a Java program that asks the user to enter “bus” or “subway” or “walk”. If the user enters “bus” display “$1.00”. If they enter “subway” display “$1.50 and if they enter “walk” display “Free”.
B. Write a java program that creates the following two arrays:
String[] candidates = {“S Jones”,”Justin Fairfax”,”Clark Duncan”};
int[] votes = {7345,4324,3211};
Write the code that will search the votes array for the candidate with the largest number of votes and prints the name of that candidate.
QUESTION (A):-
import java.util.Scanner;
public class Demo {
public static void main(String args[]) {
Scanner sc=new Scanner(System.in);
System.out.println("enter"+"bus"+"or "+"Subway"+" or " +"walk ");
String str=sc.next();
if(str.equals("bus"))
System.out.println("$1.00");
else if(str.equals("subway"))
System.out.println("$1.50");
else if(str.equals("walk"))
System.out.println("Free");
}
}
QUESTION(B):-
public class MyClass {
public static void main(String args[]) {
String[] candidates = {"S Jones","Justin Fairfax","Clark Duncan"};
int[] votes = {7345,4324,3211};
int max=votes[0];
int index=0;
for(int i=0;i<3;i++)
{
if(max<votes[i])
{
max=votes[i];
index=i;
}
}
System.out.println("Name of the candidate is "+candidates[index]);
}
}
Get Answers For Free
Most questions answered within 1 hours.