Unit 3 and Unit 4 HW
public int getChargeStartTime(int chargeTime){
int startTime = 0;
for (int i = 1; i < 24; i++){
if (this.getChargingCost(i, chargeTime) < this.getChargingCost(startTime, chargeTime)){
startTime = i;
}
}
return startTime;
}
/** Returns true if the digits in this Digits object are in strictly increasing order;
* false otherwise.
*/
public boolean isStrictlyIncreasing(){
for(int i = 1; i < digitList.size(); i++){
if (digitList.get(i-1).compareTo(digitList.get(i)) >= 0){
return false;
}
else{
return true;
}
}
}
public boolean isBalanced(ArrayList<String> delimiters) {
int numOpen = 0
int numClosed = 0;
for(String d:delimiters){
if(d.equals(openDel)){
numOpen++;
}
if(d.equals(closeDel)){
numClosed++;
}
}
if(numOpen == numClosed){
return true;
}
else{
return false;
}
}
import java.util.Scanner;
public class GFG {
public static void
guessingNumberGame()
{
// Scanner Class
Scanner sc = new Scanner(System.in);
// Generate the numbers
int number = 1 + (int)(100 * Math.random());
int K = 5;
int i, guess;
System.out.println(
"A number is chosen"
+ " between 1 to 100."
+ "Guess the number"
+ " within 5 trials.");
// Iterate over K Trials
for (i = 0; i < K; i++) {
System.out.println("Guess the number:");
guess = sc.nextInt();
if (number == guess) {
System.out.println("Congratulations!"+ " You guessed the number.");
break;
}
else if (number > guess && i != K - 1) {
System.out.println("The number is " + "greater than " + guess);
}
else if (number < guess && i != K - 1) {
System.out.println("The number is" + " less than " + guess);
}
}
if (i == K) {
System.out.println("You have exhausted " + K + " trials.");
System.out.println("The number was " + number);
}
}
public static void main(String arg[])
{
// Function Call
guessingNumberGame();
}
}
GFG.main(null);