Question: Programming Exercise 1 (LAB01): Write a program that accepts the names of three political parties [American Union Party, National Party, and Welfare Party] and the
Programming Exercise 1 (LAB01): Write a program that accepts the names of three political parties [American Union Party, National Party, and Welfare Party] and the number of votes each received in the last mayoral election (make that up). Display the percentage of the vote each party received.
Hints for this Lab: Two (2) documents [Word doc and Java code] are attached that will help you get started with user input from the keyboard. Download and use.
An example of the program is shown below:
Enter name for first party >> American Union Party
Enter votes received >> 87
Enter name for second party >> National Party
Enter votes received >> 54
Enter name for third party >> Welfare Party
Enter votes received >> 111
The Pool Party party got 34.523809523809526 percent of the vote
The House Party party got 21.428571428571427 percent of the vote
The Surprise Party party got 44.047619047619044 percent of the vote
/** (**********************************************************************************************)
If any java assignment requires User Input, you may use the codes below to get started. The Scanner class of the java.util package
is used to read input data from different sources like input streams, users, files, etc....This means that you must import this class.
Scanner input = new Scanner(System.in); Will create an object of Scanner named input
The System.in - parameter is used to take input from the standard input. It works just like taking inputs from the keyboard.
@author Dr. Egedigwe
@version 1.0
@since 2020-03-31
(***********************************************************************************************) */
import java.util.Scanner;
public class MyClassName {
//********************************
public static void main (String args[]) {
/** This is the main method which makes use of Java Scanner Class to accept user inputs.
* You may remove and/or add to this based on your objective.
* @param args Unused.
* @return Nothing.
*/
String sVar; int iVar; char cVar; float fVar; double dVar;
Scanner input = new Scanner(System.in); //Intantiate/create an object of Scanner named input
System.out.print("Enter your sthg String >> ");
sVar = input.nextLine(); //nextLine method of the Scanner class is used to read a line of text/String
System.out.print("Enter your sthg 1 letter>> ");
cVar = input.nextLine().charAt(0); //string but 1 char - 1st position
System.out.print("Enter your sthg integer >>");
iVar = input.nextInt(); //Integer
//System.out.print("Enter your sthg double >>");
//dVar = input.nextDouble(); //Double
//System.out.print("Enter your sthg float >>");
//fVar = input.nextFloat(); //Float, nextLong(), nextShort/Byte()
/* The line that follows is used to consume the
input using the Scanner class when nextLine() method call follows */
input.nextLine();
} //end for main() method
} //end for Class
(**********************************************)
//You may also want to create a separate module/method to get the User Input separate from main():
public static String getkbdInput() {
/** This is method will get and return the User Input.
* You may remove and/or add to this based on your objective.
* @param None.
* @return strWord.
*/
String strWord;
Scanner input = new Scanner(System.in); //create an object of Scanner named input
System.out.print("Enter somethingg >> ");
strWord = input.nextLine(); //String
//System.out.print("Enter your gpa >>");
//gpa = input.nextInt(); //Integer
//System.out.print("Enter your sthg >>");
//gpadou = input.nextDouble(); //Double
//System.out.print("Enter your sthg >>");
//gpadou = input.nextFloat(); //Float, nextLong(), nextShort/Byt
// Phrase is converted to lower case before returing it.
return strWord.toLowerCase();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
