//Your programs should not crash under any circumstances.
import java.util.Scanner;
public class LetterCount
{
public static int countBs(String word)
{
for (int i = 0; i < word.length(); i++)
{
int counter = 0;
if ((word.charAt(i) == 'b') || (word.charAt(i) == 'B'))
{
counter++;
}
}
return counter;
}
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
System.out.printf("Please enter a word: ");
String str = in.next();
int result = countBs(str);
System.out.printf("%s contains letter B %d times.\n", str, result);
}
}
The program above is an INCORRECT attempt to write a program that:
In a file called LetterCount.java, modify the above program so that it works correctly.
IMPORTANT: You are NOT allowed to modify in any way the main function.
Your programs should not crash under any circumstances.
For example: if the user enters "Babylon", your program output should look EXACTLY like this:
Please enter a word: Babylon Babylon contains letter B 2 times.
As another example: if the user enters "airplane", your program output should look EXACTLY like this:
Please enter a word: airplane airplane contains letter B 0 times.
As another example: if the user enters "$%^AaBb1234", your program output should look EXACTLY like this:
Please enter a word: $%^AaBb1234 airplane contains letter B 2 times.
import java.util.Scanner;
public class LetterCount {
public static int countBs(String word) {
int counter = 0;
for (int i = 0; i < word.length(); i++) {
if ((word.charAt(i) == 'b') || (word.charAt(i) == 'B')) {
counter++;
}
}
return counter;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("Please enter a word: ");
String str = in.next();
int result = countBs(str);
System.out.printf("%s contains letter B %d times.\n", str, result);
}
}

//Your programs should not crash under any circumstances. import java.util.Scanner; public class LetterCount { public static...
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next(); String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word); if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...
Fix this program
package chapter8_Test;
import java.util.Scanner;
public class Chapter8
{
public static void main(String[] args)
{
int[] matrix = {{1,2},{3,4},{5,6},{7,8}};
int columnChoice;
int columnTotal = 0;
double columnAverage = 0;
Scanner input = new Scanner(System.in);
System.out.print("Which column would you like to average (1 or
2)? ");
columnChoice = input.nextInt();
for(int row = 0;row < matrix.length;++row)
{
columnTotal += matrix[row][columnChoice];
}
columnAverage = columnTotal / (float) matrix.length;
System.out.printf("\nThe average of column %d is %.2f\n",
columnAverage, columnAverage);
}
}
This program...
import java.util.Scanner; public class MPGMain { public static void main(String[] args) { Scanner input = new Scanner(System.in); Mileage mileage = new Mileage(); System.out.println("Enter your miles: "); mileage.setMiles(input.nextDouble()); System.out.println("Enter your gallons: "); mileage.setGallons(input.nextDouble()); System.out.printf("MPG : %.2f",mileage.getMPG()); } } public class Mileage { private double miles; private double gallons; public double getMiles()...
import java.util.Scanner; import java.util.ArrayList; public class P3A2_BRANDT_4005916 { public static void main(String[] args) { String name; String answer; int correct = 0; int incorrect = 0; Scanner phantom = new Scanner(System.in); System.out.println("Hello, What is your name?"); name = phantom.nextLine(); System.out.println("Welcome " + name + "!\n"); System.out.println("My name is Danielle Brandt. " +"This is a quiz program that...
Project 7-3 Guessing Game import java.util.Scanner; public class GuessNumberApp { public static void main(String[] args) { displayWelcomeMessage(); // create the Scanner object Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // generate the random number and invite user to guess it int number = getRandomNumber(); displayPleaseGuessMessage(); // continue until the user guesses the number int guessNumber = 0; int counter = 1; while (guessNumber != number) { // get a valid int from user guessNumber...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
import java.util.Random; import java.util.ArrayList; /** * */ public class hw5_task8 { public static void main(String[] args) { int[] grades = randomIntArr(10); printIntArray("grades", grades); ArrayList<Integer> indexesF_AL = selectIndexes_1(grades); System.out.println(" indexesF_AL: " + indexesF_AL); int[] indexesF_Arr = selectIndexes_2(grades); printIntArray("indexesF_Arr",indexesF_Arr); } public static int[] randomIntArr(int N){ int[] res = new int[N]; Random r = new Random(0); for(int i = 0; i < res.length; i++){ res[i] = r.nextInt(101); // r.nextInt(101) returns an in in range [0, 100] } return res; } public static void...
package cards; import java.util.ArrayList; import java.util.Scanner; public class GamePlay { static int counter = 0; private static int cardNumber[] = {1,2,3,4,5,6,7,8,9,10,11,12,13}; private static char suitName[] = { 'c', 'd', 'h', 's' }; public static void main(String[] args) throws CardException, DeckException, HandException { Scanner kb = new Scanner(System.in); System.out.println("How many Players? "); int numHands = kb.nextInt(); int cards = 0; if (numHands > 0) { cards = 52 / numHands; System.out.println("Each player gets " + cards + " cards\n"); } else...
the RollDie.java:
import java.util.Random;
import java.util.Scanner;
/*
* HEADER HERE !!
*/
public class RollDie {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
// Step 1: Declare and initialize array and
// initialize random number generator
int[] rollValueCounts = new int[7];
Random randGen = new Random();
// Step 2: Determine the number of rolls
System.out.print("How many times do you want to roll the die?");
System.out.print(" [Max value is 100] ");
int numRolls = scnr.nextInt();
// TODO:...
Below is the code from LE 6.2 that LE 7.1 is referring to.
import java.util.Scanner;
public class lastNameLE62 {
private static Scanner in = new Scanner(System.in);
private static int size;
public static void arraySize() {
System.out.printf("How many sports are you interested in? ");
size = Integer.parseInt(in.nextLine());
}
public static String[] setFavoriteSports() {
String []favSports = new String[size];
for(int i=1; i<=size; i++) {
System.out.printf("Enter sport #%d: ", i);
favSports[i-1] = in.nextLine();
}
return favSports;
}
public static String[] setFavoriteTeams(String[] sports) {...