In: Computer Science
Write Java code for each of the following problem
a. Two players, a and b, try to guess the cost of an item.
Whoever gets closest to the price without going over is the winner.
Return the value of the best bid. If both players guessed too high,
return -1.
example:
closestGuess(97, 91, 100) → 97
closestGuess(3, 51, 50) → 3
closestGuess(12, 11, 10) → -1
b. Given a non-empty string, return true if at least half of the
characters in the string are the lowercase letter z.
example:
zMajority("az") → true
zMajority("zabzc") → false
zMajority("w") → false
c. Given 4 numbers, return the value of the second largest
number. Note that if two or more numbers are tied for largest, then
the second largest value is also the largest value.
example:
secondLargest(1, 2, 3, 4) → 3
secondLargest(3, 2, 1, 4) → 3
secondLargest(5, 7, 5, 9) → 7
1)
input code:
output:
code:
import java.util.*;
public class Main
{
/*make a method*/
public static int closestGuess(int g1,int g2,int price)
{
/*if both greater than return -1*/
if(g1>price && g2>price)
{
return -1;
}
else if(g1>price)
{
/*if g1 greater than return g2*/
return g2;
}
else if(g2>price)
{
/*if g2 greater than return g1*/
return g1;
}
else
{
/*else return close*/
if(price-g1>price-g2)
{
return g2;
}
else
{
return g1;
}
}
}
public static void main(String[] args)
{
/*call the function*/
System.out.println("Output of
closestGuess(97,91,100): "+closestGuess(97,91,100));
System.out.println("Output of
closestGuess(3,51,50): "+closestGuess(3,51,50));
System.out.println("Output of
closestGuess(12,11,10): "+closestGuess(12,11,10));
}
}
2)
input code:
output:
code:
import java.util.*;
public class Main
{
/*make a method*/
public static boolean zMajority(String str)
{
/*declare the variables*/
int count=0;
/*count the z*/
for(int i=0;i<str.length();i++)
{
/*same than increment count*/
if(str.charAt(i)=='z')
{
count++;
}
}
/*if atleast half than return true*/
if(count>=(str.length()+1)/2)
{
return true;
}
else
{
/*else return false*/
return false;
}
}
public static void main(String[] args)
{
/*call the function*/
System.out.println("Output of
zMajority(az): "+zMajority("az"));
System.out.println("Output of
zMajority(zabzc): "+zMajority("zabzc"));
System.out.println("Output of
zMajority(w): "+zMajority("w"));
}
}
3)
input code:
output:
code:
import java.util.*;
public class Main
{
/*make a method*/
public static int secondLargest(int e,int b,int c,int d)
{
/*store value into array*/
int a[]={e,b,c,d};
/*initialize variable*/
int largest=-999;
int secondlarge=-999;
/*find secondlarge*/
for(int i=0;i<4;i++)
{
/*if grater than largest than*/
if(a[i]>largest)
{
/*store largest into secondlarge*/
secondlarge=largest;
largest=a[i];
}
else if(a[i]>secondlarge)
{
/*else if grater than secondlarge than store into
secondlarge*/
secondlarge=a[i];
}
else if(a[i]==secondlarge)
{
/*if same as secondlarge than store largest into
secondlarge*/
secondlarge=largest;
}
}
return secondlarge;
}
public static void main(String[] args)
{
/*call the function*/
System.out.println("Output of
secondLargest(1, 2, 3, 4): "+secondLargest(1,2,3,4));
System.out.println("Output of
secondLargest(3,2,1,4): "+secondLargest(3,2,1,4));
System.out.println("Output of
secondLargest(5,7,5,9): "+secondLargest(5,7,5,9));
}
}