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...
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
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...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of...
USE GENERICS TO WRITE THE JAVA CODE FOR THIS ASSIGNMENT In this assignment, rewrite two of the following sorting methods (Insertion Sort, Selection Sort, Quick Sort, and Merge Sort) to sort ArrayList of objects using Comaprable interface. (60 points)
// 5. Predict what the following code will print (write it down), then try it. var...
// 5. Predict what the following code will print (write it down), then try it. var myVariable = "global"; var myOtherVariable = "global"; function myFunction() { var myVariable = "local"; return myVariable; } function myOtherFunction() { myOtherVariable = "local"; return myOtherVariable; javascript
Write in Java. Separate code for each question. The answer to the first question should NOT...
Write in Java. Separate code for each question. The answer to the first question should NOT be comparable. 1. Write a class to represent a AlternativeEnergyCar. Select the fields and methods that fit the modeling of an alternative energy car. Make sure to include code for the constructors, set/get methods, a toString() method. 2. Inheritance – Create two abstract subclasses of AECar, one for Electric cars and one for Altfuel cars. Next create four additional subclasses., two for types of...
write JAVA code with the following condition Write the pseudocode for a new data type MyStack...
write JAVA code with the following condition Write the pseudocode for a new data type MyStack that implements a stack using the fact that you have access to a queue data structure with operations enqueue(), dequeue(), isEmpty(). Remember that every stack should have the operations push() and pop(). Hint: use two queues, one of which is the main one and one is temporary. Please note that you won’t be able to implement both push() and pop() in constant time. One...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT