Question

In: Computer Science

JAVA PLEASE Creating Classes Create a class that will keep track of a yes/no survey. You...

JAVA PLEASE

Creating Classes

Create a class that will keep track of a yes/no survey. You will have to store the text (question) of the survey plus the number of yes/no votes. The constructor requires a description of the survey to be passed int (the question). The vote method will simply increment the correct vote count. The toString method will format all of the instance data to be displayed on the screen

Solutions

Expert Solution

Note: Could you plz go through this code and let me know if u need any changes in this.Thank You
_________________

// Survey.java

import java.util.Scanner;

public class Survey {
   //Declaring instance variables
   private String ques;
   private int yesCnt;
   private int noCnt = 0;

   /**
   * @param ques
   * @param yesCnt
   * @param noCnt
   */
   public Survey(String ques) {
       this.ques = ques;
       this.yesCnt = yesCnt;
       this.noCnt = noCnt;
   }

   public void takeSurvey() {

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);
       System.out.print(ques + " (y/n):");
       char ch = sc.next(".").charAt(0);
       if (ch == 'y' || ch == 'Y') {
           yesCnt++;
       } else {
           noCnt++;
       }

   }

   @Override
   public String toString() {
       return ques + ", yesCnt=" + yesCnt + ", noCnt=" + noCnt;
   }
}
______________________________

public class Test {

   public static void main(String[] args) {
      
   Survey s=new Survey("Do you like crime movies ? ");

//Taking survey among 20 people

       for(int i=0;i<20;i++)
       {
           s.takeSurvey();
       }

           System.out.println(s);
      
   }

}
____________________________

Output:

Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):n
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):y
Do you like crime movies ? (y/n):n
Do you like crime movies ? , yesCnt=10, noCnt=10

_______________Could you plz rate me well.Thank You


Related Solutions

/////////////////JAVA PLEASE///////////////////////////////// Create a class called GVdate to keep track of a calendar date including month,...
/////////////////JAVA PLEASE///////////////////////////////// Create a class called GVdate to keep track of a calendar date including month, day and year.  You can do simple things like checking if it is your birthday, advancing to the next day, checking if a given date is valid and checking if it is a leap year. Class Fields/Instance Variables Provide appropriate names and data types for each of the private instance variables: the month, day and year (int) two final integers that represent YOUR birthday (month...
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of...
Create JAVA PROGRAM, and write comment for codes also. 3) You want to keep track of your progress towards running a 10K. There are two kinds of races - 5K and 10K. The program needs to ask what race was run and what the time was in seconds until the user quits. When they quit, display the average and best time for each type of race in minutes.
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that...
Using JAVA: This assignment is about aggregation and class collaboration. You will create several Classes that will be part of an overall class named InstrumentDisplay. The classes are FuelGage, Odometer, MilesSinceLastGas, and CruisingRange. The FuelGage should assume a 15 gallon tank of gasoline and an average consumption of 1 gallon every 28 miles. It should increment in 1 gallon steps when you "add gas to the tank". It should decrement by 1 every 28 miles. It should display its current...
Java program Create two classes based on the java code below. One class for the main...
Java program Create two classes based on the java code below. One class for the main method (named InvestmentTest) and the other is an Investment class. The InvestmentTest class has a main method and the Investment class consists of the necessary methods and fields for each investment as described below. 1.The Investment class has the following members: a. At least six private fields (instance variables) to store an Investment name, number of shares, buying price, selling price, and buying commission...
You are going to create a console based program to keep track of a small business...
You are going to create a console based program to keep track of a small business that sells Doodads. First you will need to create a class Doodad that keeps track of two integers and two Strings. Next, create a constructor for the Doodad. Next, add getters and setters for each of the fields (two integers and two Strings). You need to use Doodad.java (see the starter code) Inside your main method ask the user to read in the two...
C++ Please. Break it down barney style if possible. Instructions Create a program to keep track...
C++ Please. Break it down barney style if possible. Instructions Create a program to keep track of the statistics for a kid’s soccer team. The program will have a structure that defines what data the program will collect for each of the players. The structure will keep the following data: Players Name (string) Players Jersey Number (integer) Points scored by Player (integer) The program will have an array of 12 players (use less for testing and development, use a constant...
Write a class to keep track of a balance in a bank account with a varying...
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to...
Write a class to keep track of a balance in a bank account with a varying...
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to...
Write a class to keep track of a balance in a bank account with a varying...
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero).The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to be...
Write a class to keep track of a balance in a bank account with a varying...
Write a class to keep track of a balance in a bank account with a varying annual interest rate. The constructor will set both the balance and the interest rate to some initial values (with defaults of zero). The class should have member functions to change or retrieve the current balance or interest rate. There should also be functions to make a deposit (add to the balance) or withdrawal (subtract from the balance). You should not allow more money to...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT