Question

In: Computer Science

Write Java code for each of the following problem a. Two players, a and b, try...

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

Solutions

Expert Solution

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



Related Solutions

Java please! Write the code in Exercise.java to meet the following problem statement: Write a program...
Java please! Write the code in Exercise.java to meet the following problem statement: Write a program that will print the number of words, characters, and letters read as input. It is guaranteed that each input will consist of at least one line, and the program should stop reading input when either the end of the file is reached or a blank line of input is provided. Words are assumed to be any non-empty blocks of text separated by spaces, and...
PYTHON: Write the code to play a card game called Battle. Two players each have a...
PYTHON: Write the code to play a card game called Battle. Two players each have a card deck consisting of the following cards: two, three, four, … jack, queen, king, ace, in increasing order. One card deck could be represented by a list such as: cardsPlayer1 = ["two", "three", "four"..."jack", "queen", "king", "ace"] Both players have a card randomly selected. When a card is selected, remove it from the player’s deck. The player that plays the higher of the two...
As in previous labs, please write the Java code for each of the following exercises in...
As in previous labs, please write the Java code for each of the following exercises in BlueJ. For each exercise, write a comment before the code stating the exercise number. Make sure you are using the appropriate indentation and styling conventions Exercise 1 (15 Points) In BlueJ, create a new project called Lab6 In the project create a class called Company Add instance data (fields) for the company name (a String) and the sales for the current period (a double)...
Write the recurrence for each of the following two problems. Try to come up with the...
Write the recurrence for each of the following two problems. Try to come up with the solution using your understanding. 1) Maximal subarray problem 2) Weighted interval selection problem
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a...
Java Problem: Please answer both parts of the question fully: (a). Write Java code for a method to test if a LinkedList<Long> has Long values that form a Fibonacci sequence from the beginning to the end and return true if it is and false otherwise. A sequence of values is Fibonnaci if every third value is equal to sum of the previous two. Eg., 3,4,7,11,18,29 is a Fibonacci sequence whereas 1,2,3,4 is not, because 2+3 is not equal to 4....
Recursion practice Write a Java program with functions for each of the following problems. Each problem...
Recursion practice Write a Java program with functions for each of the following problems. Each problem should be solved by writing a recursive function. Your final program should not have any loops in it. All of your solutions should be in a single .java file. The main function of the file should demonstrate each of your solutions, by running several tests and producing the corresponding outputs. Write a recursive method to 1. calculate power of a give number 2. multiply...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: Create a...
Write the Java source code necessary to build a solution for the problem below: Create a MyLinkedList class. Create methods in the class to add an item to the head, tail, or middle of a linked list; remove an item from the head, tail, or middle of a linked list; check the size of the list; and search for an element in the list. Create a test class to use the newly created MyLinkedList class. Add the following names in...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci...
Write the Java source code necessary to build a solution for the problem below: The Fibonacci numbers form a sequence where each number is the sum of the previous two numbers. Starting from 0 and 1, the first eight Fibonacci numbers are built in the following list using the equation Fn = Fn-1 + Fn-2: 0, 0 0, 1 1, 1 1, 2 2, 3 3, 5 5, 8 8, 13 The sequence would be the numbers in red (0,...
Can you write code or class in java accepting from the user a + b as...
Can you write code or class in java accepting from the user a + b as a string and accepting their value from the user and the out but will be the sum of the variables.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT