Question

1. Write a Java program to count all words in a string. User inputs a string...

1. Write a Java program to count all words in a string. User inputs a string sentence and your program should count the number of words in that string.

Test Data:
Input the string: The quick brown fox jumps over the lazy dog.
Expected Output:

Number of words in the string: 9

2. Write a class called ATMTransaction. This class has a variable balance and account number. It has three methods, checkBalance, deposit and withdraws along with constructors getters and setters and a toString. Write a program that creates an object of this class. I should be able to deposit and withdraw from my account number based on my balance, but I shouldn't be able to change my account number. Look for loopholes in your program, test it correctly, your balance cannot be negative.

I am close to completing the assignment. For the Word Count. It was supposed to count the words that it was supposed to type. But if you hit nothing, it was to type out a zero, nothing, not a " 1 ". This is what I have so far. My professor said that I am missing an if else statement.

Here is the Word Count ⤵

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package wordcount;

/**

*

* @author student-ltlc126

*/

import java.util.Scanner;

public class WordCount {

  /**

    * @param args the command line arguments

    */

   

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       System.out.println("Input the string: ");

       String phrase = input.nextLine();

//        if (phrase == "") {

//            System.out.println("Number of words in the string: 0");

//        }

//        else{

System.out.printf("Number of words in the string: " + phrase.split("").length);

       // }

   }

}

======================================================================================================

And the last thing is the ATM. I have everything, but my professor said I am missing a getter and a setter. And the toString. That it is missing to include the account number. There is no string account number for them, no private double balance and account number.

Here is the code for the ATM ⤵

/*

* To change this license header, choose License Headers in Project Properties.

* To change this template file, choose Tools | Templates

* and open the template in the editor.

*/

package atmtransaction;

/**

*

* @author student-ltlc126

*/

import java.util.Scanner;

public class ATMTransaction {

   /**

    * @param args the command line arguments

    */

   private double balance;

   //add accountNumber variable

   ATMTransaction() {

       balance = 5000;

   }

     //need getter and setter

   void withdraw(double withdraw) {

       if (withdraw > 0) {

           if (balance >= withdraw) {

               balance = balance - withdraw;

               System.out.println("Collect your Money");

           } else {

               System.out.println("Error!! Negative value Entered");

           }

       } else {  

       }

   }

   //need getter and setter

   void deposit(double deposit) {

       if (deposit > 0) {

           balance = balance + deposit;

           System.out.println("Your Money is in Your Card");

           System.out.println("");

       } else {

           System.out.println("Error!! Negative Value Entered");

       }

   }   

   //need getter and setter

   void checkBalance() {

       System.out.println("Balance: " + balance);

       System.out.println();

   }

   @Override //incorporate account balance into here

   public String toString() {

       return "ATMTransaction [balance = " + balance + "]";

   }

   /**

    * @param args the command line arguments

    */

   public static void main(String[] args) {

       double withdraw, deposit;

       Scanner input = new Scanner(System.in);

       ATMTransaction myATM = new ATMTransaction();       

       while (true) {

           System.out.println(myATM.toString());

           System.out.println("Automated Teller Machine");

           System.out.println("Choose 1 to Withdraw");

           System.out.println("Choose 2 to Deposit");

           System.out.println("Choose 3 to Check Balance");

           System.out.println("Choose 4 to Exit");

           System.out.println("Choose the Operation You Want to Perform: ");             

           int n = input.nextInt();

           switch (n) {

               case 1:

                   System.out.println("Enter Money to Withdraw: ");

                   withdraw = input.nextDouble();

                   myATM.withdraw(withdraw);

                   break;

               case 2:

                   System.out.println("Enter Money To Deposit: ");

                   deposit = input.nextDouble();

                   myATM.deposit(deposit);

                   break;

               case 3:

                   myATM.checkBalance();

                   break;

               case 4:

                   System.exit(0);

           }

       }

   }

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer 1:

import java.util.Scanner;

public class WordCount {

   /**
   *
   * @param args
   * the command line arguments
   *
   */

   public static void main(String[] args) {

       Scanner input = new Scanner(System.in);

       System.out.println("Input the string: ");

       String phrase = input.nextLine();

       if (phrase.trim().length() == 0) {

           System.out.println("Number of words in the string: 0");

       }

       else {

           System.out.printf("Number of words in the string: " + phrase.split(" ").length);

       }

   }

}

Answer 2:


import java.util.Scanner;

public class ATMTransaction {

/**

* @param args the command line arguments

*/

private double balance;

//add accountNumber variable

ATMTransaction() {

balance = 5000;

}

//need getter and setter

void withdraw(double withdraw) {

if (withdraw > 0) {

if (balance >= withdraw) {

balance = balance - withdraw;

System.out.println("Collect your Money");

} else {

System.out.println("Error!! Negative value Entered");

}

} else {

}

}

//need getter and setter

void deposit(double deposit) {

if (deposit > 0) {

balance = balance + deposit;

System.out.println("Your Money is in Your Card");

System.out.println("");

} else {

System.out.println("Error!! Negative Value Entered");

}

}   

//need getter and setter

void checkBalance() {

System.out.println("Balance: " + balance);

System.out.println();

}

@Override
public String toString() {
   return "ATMTransaction [balance=" + balance + "]";
}

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

double withdraw, deposit;

Scanner input = new Scanner(System.in);

ATMTransaction myATM = new ATMTransaction();   

while (true) {

System.out.println(myATM.toString());

System.out.println("Automated Teller Machine");

System.out.println("Choose 1 to Withdraw");

System.out.println("Choose 2 to Deposit");

System.out.println("Choose 3 to Check Balance");

System.out.println("Choose 4 to Exit");

System.out.println("Choose the Operation You Want to Perform: ");   

int n = input.nextInt();

switch (n) {

case 1:

System.out.println("Enter Money to Withdraw: ");

withdraw = input.nextDouble();

myATM.withdraw(withdraw);

break;

case 2:

System.out.println("Enter Money To Deposit: ");

deposit = input.nextDouble();

myATM.deposit(deposit);

break;

case 3:

myATM.checkBalance();

break;

case 4:

System.exit(0);

}

}

}

public double getBalance() {
   return balance;
}

public void setBalance(double aBalance) {
   balance = aBalance;
}

}

Add a comment
Know the answer?
Add Answer to:
1. Write a Java program to count all words in a string. User inputs a string...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • /* * To change this license header, choose License Headers in Project Properties. * To change...

    /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package checkingaccounttest; // Chapter 9 Part II Solution public class CheckingAccountTest { public static void main(String[] args) {    CheckingAccount c1 = new CheckingAccount(879101,3000); c1.deposit(350); c1.deposit(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.withdraw(350); c1.withdraw(220); c1.withdraw(100); c1.deposit(1100); c1.deposit(700); c1.deposit(1000); System.out.println(c1); System.out.println("After calling computeMonthlyFee()"); c1.computeMonthlyFee(); System.out.println(c1); } } class BankAccount { private int...

  • How would you write the following program using switch statements instead of if-else statements (in java)...

    How would you write the following program using switch statements instead of if-else statements (in java) import java.util.*; public class ATM { public static void main(String[] args) { Scanner input = new Scanner (System.in); double deposit, withdrawal; double balance = 6985.00; //The initial balance int transaction; System.out.println ("Welcome! Enter the number of your transaction."); System.out.println ("Withdraw cash: 1"); System.out.println ("Make a Deposit: 2"); System.out.println ("Check your balance: 3"); System.out.println ("Exit: 4"); System.out.println ("***************"); System.out.print ("Enter Your Transaction Number "); transaction...

  • please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank...

    please rewrite this code as Pseudo-Code,.. basically rewrite the code but in english language , Thank you so much! public abstract class BankAccount {    //declare the required class variables    private double balance;    private int num_deposits;    private int num_withdraws;    private double annualInterest;    private double serviceCharges;       //constructor that takes two arguments    // one is to initialize the balance and other    // to initialize the annual interest rate       public BankAccount(double balance,...

  • I would like someone to check my code and help with my for loop to print...

    I would like someone to check my code and help with my for loop to print the recipe. It is incorrect. package SteppingStones; import java.util.Scanner; //Scanner class// import java.util.ArrayList; //ArrayList// import ingredients.Ingredient; /**Gets from package ingredients and class Ingredient * * @author kimbe */ public class SteppingStone5_Recipe {    //Instance Variables// private ArrayList recipeIngredients; private String recipeName; private int servings; private double totalRecipeCalories;                   //Setter and Getters//    public ArrayList getrecipeIngredients() { return recipeIngredients; }...

  • Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems....

    Use the Java codes below (Checking Account and Checking Account Demo), and work on these problems. You have to do both of your java codes based on the two provided Java codes. Create one method for depositing and one for withdrawing. The deposit method should have one parameter to indicate the amount to be deposited. You must make sure the amount to be deposited is positive. The withdraw method should have one parameter to indicate the amount to be withdrawn...

  • NOTE: Use the Account class codes in the section below these questions. Modify the main method...

    NOTE: Use the Account class codes in the section below these questions. Modify the main method in the CheckingAccountDemo class: Take out all previous code. Declare an object array with 10 accounts. Create a for loop to ask user to input each account’s information (name, account number, and initial balance) from keyboard and then initialize for each account object. Create a while loop to allow one to work on depositing and withdrawing operations on any account till one want to...

  • If I enter a negative value the program throws an error and the withdrawal amount is...

    If I enter a negative value the program throws an error and the withdrawal amount is added to the balance please help me find why? public abstract class BankAccount { private double balance; private int numOfDeposits; private int numOfwithdrawals; private double apr; private double serviceCharge; //Create getter and setter methods public double getBalance() { return balance; } public void setBalance(double balance) { this.balance = balance; } public int getNumOfDeposits() { return numOfDeposits; } public void setNumOfDeposits(int numOfDeposits) { this.numOfDeposits =...

  • •create a new savings account object (for choice 2). •ask the user to enter an initial...

    •create a new savings account object (for choice 2). •ask the user to enter an initial balance which should be greater than 50. If the entered amount is less than 50, the program will ask the user to enter another amount. This continues until an amount greater than 50 is entered. •Set the balance of the savings account object by calling the setBalance() method. •Ask the user to deposit an amount. Deposit that amount and display the balance of the...

  • java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a...

    java code ============= public class BankAccount { private String accountID; private double balance; /** Constructs a bank account with a zero balance @param accountID - ID of the Account */ public BankAccount(String accountID) { balance = 0; this.accountID = accountID; } /** Constructs a bank account with a given balance @param initialBalance the initial balance @param accountID - ID of the Account */ public BankAccount(double initialBalance, String accountID) { this.accountID = accountID; balance = initialBalance; } /** * Returns the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT