Okay, the objective of this program is to input String values, such as penny, dime, quarter, and nickel, into the ArrayList purse in class Purse. Then once Purse is executed through the PurseMain class then the Coin class is supposed to take the string values of the coin names that were added into the ArrayList purse and add their dollar values together. My professor told us to create the coin values in a third Class called Coin and to create another ArrayList in PurseMain. But really my main question is, how do you call the objects that were created in the Coin Class into the ArrayList in PurseMain? Any help would be appreciated. Thanks.
package purse;
import java.util.Scanner;
import java.util.ArrayList;
/**
* The Purse program creates an ArrayList called purse that gets printed out,
reversed, and transfered into another ArrayList called purse2.
*
* - ArrayList purse
* - ArrayList purse2
* - Scanner coin - the Scanner that is used to type the contents of ArrayList purse
* - Scanner coin2- the Scanner that is used to type the contents of ArrayList purse2
* - String input - contains Scanner coin and is used to fill ArrayList purse
* - String input2- contains Scanner coin2 and is used to fill ArrayList purse2
* - String end - sentinel for ending the process inputting strings into Scanner coin and Scanner coin2
*
*/
public class Purse
{
ArrayList<String> purse = new ArrayList<>();
/**
* fills ArrayList purse and purse2 with U.S coin names
* purse gets printed out and then again is printed in reverse
* purse2 is printed
*/
public void addCoin()
{
String penny = "penny";
String nickel = "nickel";
String dime = "dime";
String quarter = "quarter";
String end = "done";
Scanner coin = new Scanner (System.in);
String input = " ";
System.out.println("Please put as many coins of U.S currency as you like into the purse, hit the ENTER button after each coin and, type 'done' when finished: ");
while (!input.equalsIgnoreCase ("done"))
{ input = ( coin.nextLine());
if (input.equalsIgnoreCase("penny")||input.equalsIgnoreCase("nickel")||input.equals IgnoreCase("dime")||input.equalsIgnoreCase("quarter")||input.equalsIgnoreCase(en d))
{
purse.add(input);
purse.remove(end);
}
else{
System.out.println("Please input a coin of U.S currency.");
}
}
}
/**
#return ArrayList purse
*/
public ArrayList<String> printPurseContents()
{
System.out.println("Contents of the purse: " + purse);
return purse;
}
/** checks whether purse2 has the same coins in the same order as purse
* #return
* #param purse2
*/
public boolean sameContents(Purse purse2)
{
if (purse2.purse.equals(purse))
{
return true;
}
else
{
return false;
}
}
/**
* checks whether purse2 has the same coins as in purse, disregarding the order
* #param purse2
* #return
*/
public boolean sameCoins(Purse purse2)
{
if( purse2.purse.containsAll(purse))
{
return true;
}
else
{
return false;
}
}
/**
* adds contents of purse into purse2 and clears purse of its contents
* #param purse2
*/
public void transfer(Purse purse2)
{
purse2.purse.addAll(purse);
purse.clear();
System.out.println("The second purse now has: " + purse2.purse );
System.out.println("and the first purse has: " + purse);
}
}
----------PurseMain
package purse;
import java.util.ArrayList;
/*
* 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.
*/
/**legalcoin = new ArrayList<Coin>; <-----
* legalcoin.add( new Coin ("Penny", .01); <--- Professor said to do
*
* #author
* 9/01/2015
* Lab I
*/
public class PurseMain
{
private ArrayList<Coin> legalCoin = new ArrayList<>();
legalCoin.add (new Coin ("Penny",0.01));
public static void main(String[] args)
{
Purse purse1 = new Purse();
purse1.addCoin();
purse1.printPurseContents();
Purse purse2 = new Purse();
purse2.addCoin();
purse2.printPurseContents();
System.out.println("Both of the purses have the same contents and the contents is in the same order: " + purse2.sameContents(purse1));
System.out.println("Both of the purses have the same contents: " +purse2.sameCoins(purse1));
}
}
----------Coin
package purse;
/*
* 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.
*/
/** private string name;
* private double value;
* sum up value method
*
* #author
*/
public class Coin
{
private String Penny = "penny";
private String Nickel = "nickel";
private String Dime = "dime";
private String Quarter = "quarter";
private double penny = 0.01;
private double nickel = 0.05;
private double dime = 0.10;
private double quarter = 0.25;
public void SumUpValue()
{
}
}
You will need to have a new Coin pouch in your purse(requirement wise). So, have a coin pouch that takes list of coins in it. Since the purse has the coin pouch, it would be a property of your purse.
public class Coin{
private String type;
private String currencyType;
//more fields and getters and setters
}
public class Purse{
ArrayList<String> purse = new ArrayList<>();
List<Coins> coinPouch = new ArrayList<>();//contains coin objects
.
.
.
.
private void addCoins(String coinType){
Coin coin = new Coin();
coin.setType(coinType);
coinPouch.add(coin);
}
}
Your modified code for adding coins would be like:
if (input.equalsIgnoreCase("penny")|| input.equalsIgnoreCase("nickel") ||input.equalsIgnoreCase("dime")||input.equalsIgnoreCase("quarter")||input.equalsIgnoreCase(end)){
addCoins(input);//Adds coins to the list of coins objects and the purse now has the coin pouch :D
purse.remove(end);
}
else{
System.out.println("Please input a coin of U.S currency.");
}
Related
Intent: The program is intended to recieve an input from the user and compare it against the contents of the ArrayList. If the input is not found, the program the input to the list and gives it a winCount of 1. The list is printed with the number of wins following it. In the end the user with the hightest wincount wins. If there is a tie a user is randomly selected.
The problem: My arrayValues are not properly stored
The Team class has been provided with the methods below :
public class Team implements Comparable<Team> {
// _-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_-v-_
// CS 1323: Implement this method
/**
* This is the method you will fix to allow you to return the contents of
* this Team item in the nice format: "name: winCount". The current
* implementation will allow the program to compile, but it returns nothing.
*
* For example: "Sooners: 3"
*
* #return the formatted String that can then be output to the console.
*/
public String toString(String team, int wins) {
String winStatement = team + ": " + wins;
return winStatement;
}
// _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
// Data fields
private String name;
private int winCount;
Team() {
name = "Sooners";
winCount = 1;
}
Team(String inputName) {
name = inputName;
winCount = 1;
}
Team(String inputName, int inputWinCount) {
name = inputName;
winCount = inputWinCount;
}
// ----------------------------------------------------
// Getters and Setters (aka Accessors and Mutators) ===
/**
* #return the name
*/
public String getName() {
return name;
}
/**
* #param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* #return the winCount
*/
public int getWinCount() {
return winCount;
}
/**
* #param winCount
* the winCount to set
*/
public void setWinCount(int winCount) {
this.winCount = winCount;
}
/**
* Increments the winCount variable by one for this Team
*/
public void incrementWinCount() {
winCount++;
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object.
*
* This method allows you to use the contains method in ArrayList to see
* if any element in an array list has the same name as a specific Team.
*
* #param o
* the other Team being compared to.
*/
#Override
public boolean equals(Object o) {
return name.equals(((Team) o).name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* #param otherTeam
* one team
*/
public boolean sameName(Team otherTeam) {
return name.equals(otherTeam.name);
}
/**
* This method allows you to check to see if this Team object has the same
* name as another Team object
*
* #param team1
* one team
* #param team2
* the other team
*/
public static boolean sameName(Team team1, Team team2) {
return team1.name.equals(team2.name);
}
/**
* This method allows you to sort an ArrayList of Team items using
* Collections.sort
*
* #param o
* the other Team being compared to.
* #return -1 if this Team item should come first, +1 if this Team item
* should come after the other, and 0 if this Team item is
* equivalent to the other.
*/
#Override
public int compareTo(Team o) {
if (this.winCount < o.winCount) {
return -1;
} else if (this.winCount > o.winCount) {
return 1;
}
return 0;
}
}
I am new to reference variables and ArrayLists, so I suspect the error has something to do with them. The code is below :
public class Project6_ReeceWitcher
{
public static void main(String args[])
{
Scanner scnr = new Scanner(System.in);
Random rando = new Random();
String name = "hi";
int cycles = 0;
int value = 0;
ArrayList<Team> teams = new ArrayList<Team>();
Team myTeam = new Team();
Team thisTeam = new Team(name);
System.out.println("Welcome to the Advanced Sportsball Tracker!");
while (!name.equals("x")) // looping print statement
{ // x loop begins
System.out.println("Which team just won? (x to exit)");
name = scnr.next();
if (!teams.equals(name))
{
teams.add(thisTeam);
myTeam.setWinCount(1);
}
else if (teams.equals(name))
{
myTeam.incrementWinCount();
}
cycles++;
}// x loop ends
if (cycles == 1) // prints no data if user immediately exits
{
System.out.println("No data input");
}
System.out.println("Final Tally: "); // loop to print teams
for (Team team : teams) // FIXME
{
System.out.println(team);
}
The program runs and outputs these values
Welcome to the Advanced Sportsball Tracker!
Which team just won? (x to exit)
1
Which team just won? (x to exit)
2
Which team just won? (x to exit)
x
Final Tally:
Team#75b84c92
Team#75b84c92
Team#75b84c92
The winner is the with win(s)
However the intended output is
Welcome to the Advanced Sportsball Tracker!
Which team just won? (x to exit)
Sooners - these values are user inputs and are not printed
Which team just won? (x to exit)
Sooners -
Which team just won? (x to exit)
Cowboys -
Which team just won? (x to exit)
Bears -
Which team just won? (x to exit)
Bears -
Which team just won? (x to exit)
Cowboys -
Which team just won? (x to exit)
ThatOtherTeam -
Which team just won? (x to exit)
x
Final Tally:
ThatOtherTeam: 1
Sooners: 2
Cowboys: 2
Bears: 2
The winner is the Cowboys with 2 win(s)!
Thank you for the assistance!
Hey guys I have been working on this program for class the past couple hours and just cant seem to get these last 2 issues resolved. Its basically a slightly modified CashRegister class with basic functions through a menu. The problems I am having are:
1) After the user makes a menu selection for the first time, every subsequent time the menu shows up in console twice and I cant seem to find a fix for this.
2) Also whenever I choose to display the content of my CashRegister, the first line is always output as 0.00 no matter the input.
Here is my CashRegister class followed by my tester:
import java.util.ArrayList;
/**
*
*/
/**
* #author Cole
*
*/
public class CashRegister {
private double dailyTotal;
private double totalPrice;
ArrayList<Double> items;
/**
Constructs a cash register with cleared item count and total.
*/
public CashRegister()
{
items = new ArrayList<Double>();
dailyTotal = 0;
totalPrice= 0;
}
/**
Adds an item to this cash register.
#param price the price of this item
*/
public void addItem(double price)
{
items.add(price);
dailyTotal = dailyTotal + price;
}
/**
Gets the price of all items in the current sale.
#return the total amount
*/
public double getTotal()
{
for(int x=0; x<items.size(); x++){
totalPrice = totalPrice + items.get(x);
}
return totalPrice;
}
/**
Gets the number of items in the current sale.
#return the item count
*/
public int getCount()
{
return items.size();
}
/**
Clears the item count and the total.
*/
public void clear()
{
items.clear();
totalPrice = 0;
}
public void display(){
for(int x=0; x<items.size(); x++){
System.out.printf("%10.2f%n", items.get(x));
}
System.out.println("------------------------------");
}
public double getDailyTotal(){
dailyTotal = dailyTotal + totalPrice;
return dailyTotal;
}
}
import java.util.ArrayList;
import java.util.Scanner;
/**
*
*/
/**
* #author Cole
*
*/
public class Prog2 {
/**
* #param args
*/
public static final String MENU = "******************************************\n" +
"* Enter \"n\" to start a new Cash Register. *\n" +
"* Enter \"a\" to add an item to the current Cash Register. *\n" +
"* Enter \"d\" to display the total of the current Cash Register. *\n" +
"* Enter \"e\" to exit the program. *\n" +
"******************************************";
public static final String NEW_CUSTOMER = "n";
public static final String ADD_ITEM = "a";
public static final String DISPLAY = "d";
public static final String EXIT = "e";
public static void main(String[] args) {
// TODO Auto-generated method stub
CashRegister register = new CashRegister();
Scanner keyboard = new Scanner(System.in);
String input;
double userInput = 0;
do {
input = printMenu(keyboard);
if(input.equals(NEW_CUSTOMER)){
register.getDailyTotal();
register.clear();
}
else if(input.equals(ADD_ITEM)){
System.out.println("Please enter the price of the item: ");
register.addItem(userInput);
userInput = keyboard.nextDouble();
}
else if(input.equalsIgnoreCase(DISPLAY)){
register.display();
System.out.println("Total: " + register.getTotal());
System.out.println("Item Count: " +register.getCount());
}
else if(input.equalsIgnoreCase(EXIT)){
System.out.printf("Daily Sales Total: " + "%.2f%n",register.getDailyTotal());
System.out.println("Program Ended...");
break;
}
}while(input != EXIT);
}
public static String printMenu(Scanner input){ //this method displays the menu for the user
String response = "no reponse yet";
System.out.println(MENU);
response = input.nextLine();
return response; //response returned based on the users input
}
}
You need to get input from the user before you add the item, that's why you are getting a 0 for your first item. Since your value for userInput is set to 0 at the beginning and your statements are switched you will always initialy create an item with 0.0 for it's value and all the other values will be one step behind the actual inputs.
else if(input.equals(ADD_ITEM)){
System.out.println("Please enter the price of the item: ");
userInput = keyboard.nextDouble();
register.addItem(userInput);
}
I missed points for putting implementation in the driver class, rather than having the driver class only make calls methods in other classes. I'm not sure how to move the implementation outside of the main class. Suggestions welcome!
/*
*Deitel Chapter 6 Exercise 6.30
*/
package guess;
import java.util.Scanner;
/**
*Driver class for number guessing game
*/
public class GuessDriver {
/**
*
* #param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*
*instantiate guess class
*/
Guess Guess = new Guess();
/*
*loops program until user guesses correct number
*/
while (Guess.getGuess() != Guess.secretNumber){
/*
*takes input from user
n*/
System.out.println("Enter a number from 1 to 1000: ");
int setGuess = input.nextInt();
Guess.setGuess(setGuess);
/*
*nested if-else statements compares guess against secret number
*/
if(Guess.secretNumber < Guess.guess){
System.out.println("too high");
} else if (Guess.secretNumber > Guess.guess){
System.out.println("too low");
} else {
System.out.println("Congratulations, you win!");
}
}
}
}
/*
*Deitel Chapter 6 Exercise 6.30
*/
package guess;
import java.util.Random;
/**
* Takes in and stores an integer, sets value for random number
*/
public class Guess {
/**
*instance variable
*/
public int guess;
/**
*instance variable
*/
public int secretNumber = (int) (Math.random() * 999 + 1);
/**
*return secret number
* #return
*/
public int getSecretNumber() {
return secretNumber;
}
/**
*return guess
* #return
*/
public int getGuess() {
return guess;
}
/**
*set guess
* #param guess
*/
public void setGuess(int guess) {
this.guess = guess;
}
}
Well, first of all. I would suggest to make private all variables of the Guess class. (See that you have a getSecretNumber() method that already gives you the value of the secretNumber variable.
Secondly, you could create a public method like checkGuessing(int guess) in the Guess class. This method would take the user input, and write the answer on console ("too high", "too low", and so on).
Another option could be to add this same functionality to the setGuess method.
Finally, to ease future debugging, you should name the classes with Uppercase letters, the variables (even the instances of classes) with camelCase letters, and try that class's variables be named different than the name of the class.
So the final code could be something like this:
in GuessDriver class:
package guessdriver;
import java.util.Scanner;
/**
*Driver class for number guessing game
*/
public class GuessDriver {
/**
*
* #param args
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
/*
*instantiate guess class
*/
Guess guess = new Guess();
/*
*loops program until user guesses correct number
*/
while (guess.checkGuessing()==false){
/*
*takes input from user
n*/
System.out.println("Enter a number from 1 to 1000: ");
int setGuess = input.nextInt();
guess.setGuess(setGuess);
}
}
}
And in the Guess class:
package guessdriver;
public class Guess {
/**
*instance variable
*/
private int guessing;
/**
*instance variable
*/
private int secretNumber = (int) (Math.random() * 999 + 1);
/**
*return secret number
* #return
*/
public int getSecretNumber() {
return secretNumber;
}
/**
*return guess
* #return
*/
public int getGuess() {
return guessing;
}
/**
*set guess
* #param guess
*/
public void setGuess(int guessing) {
this.guessing = guessing;
}
public boolean checkGuessing(){
if(secretNumber < guessing){
System.out.println("too high");
return false;
} else if (secretNumber > guessing){
System.out.println("too low");
return false;
} else {
System.out.println("Congratulations, you win!");
return true;
}
}
}
I'm creating a small 'game' program where a player enters a floor/room number, but when the player guess it gets stuck and loops on a single player and doesn't move to the next player and doesn't tell if the player is correct or incorrect as a guess where the dog is being held in the building.
PuppyPlay.java:
import java.util.Random;
import java.util.Scanner;
/**
* This program is used as a driver program to play the game from the
* class LostPuppy.
*
* A puppy is lost in a multi-floor building represented in the class
* LostPuppy.class. Two players will take turns searching the building
* by selecting a floor and a room where the puppy might be.
*
*/
public class PuppyPlay{
/**
* Driver program to play LostPuppy.
*
* #param theArgs may contain file names in an array of type String
*/
public static void main(String[] theArgs){
Scanner s = new Scanner(System.in);
LostPuppy game;
int totalFloors;
int totalRooms;
int floor;
int room;
char[] players = {'1', '2'};
int playerIndex;
boolean found = false;
Random rand = new Random();
do {
System.out.print("To find the puppy, we need to know:\n"
+ "\tHow many floors are in the building\n"
+ "\tHow many rooms are on the floors\n\n"
+ " Please enter the number of floors: ");
totalFloors = s.nextInt();
System.out.print("Please enter the number of rooms on the floors: ");
totalRooms = s.nextInt();
s.nextLine(); // Consume previous newline character
// Start the game: Create a LostPuppy object:
game = new LostPuppy(totalFloors, totalRooms);
// Pick starting player
playerIndex = rand.nextInt(2);
System.out.println("\nFloor and room numbers start at zero '0'");
do {
do {
System.out.println("\nPlayer " + players[playerIndex]
+ ", enter floor and room to search separated by a space: ");
floor = s.nextInt();
room = s.nextInt();
//for testing, use random generation of floor and room
//floor = rand.nextInt(totalFloors);
//room = rand.nextInt(totalRooms);
} while (!game.indicesOK(floor, room)
|| game.roomSearchedAlready(floor, room));
found = game.searchRoom(floor, room, players[playerIndex]);
playerIndex = (playerIndex + 1) % 2;
System.out.println("\n[" + floor + "], [" + room + "]");
System.out.println(game.toString());
s.nextLine();
} while (!found);
playerIndex = (playerIndex + 1) % 2;
System.out.println("Great job player " + players[playerIndex] +"!");
System.out.println("Would you like to find another puppy [Y/N]? ");
} while (s.nextLine().equalsIgnoreCase("Y"));
}
}
LostPuppy.java:
import java.util.Random; // Randomize the dog placement in building
import java.util.Scanner; // User input
/**
* This program is used as a program to play the game from the
* driver PuppyPlay.java
*
* A puppy is lost in a multi-floor building represented in the class
* LostPuppy.class. Two players will take turns searching the building
* by selecting a floor and a room where the puppy might be.
*
*/
public class LostPuppy{
private char[][] myHidingPlaces; // Defining class fields for assignment
private int myFloorLocation;
private int myRoomLocation;
private char myWinner;
private boolean myFound;
/**
* Creates constructor takes floor/room numbers inputted by user
*
* #param theFloors for number of floors
* #param theRooms for number of rooms
*/
public LostPuppy(int theFloors, int theRooms) {
Random random = new Random();
myHidingPlaces = new char[theFloors][theRooms];
// Filling array with spaces
int i;
for (i = 0; i < theFloors; i++) {
for (int k = 0; k < theRooms; k++) {
myHidingPlaces[i][k] = ' ';
}
}
myFloorLocation = random.nextInt(theFloors);
myRoomLocation = random.nextInt(theRooms);
myHidingPlaces[myFloorLocation][myRoomLocation] = 'P';
myWinner = ' ';
myFound = false;
}
/**
* Checks if room has been searched prior
*
* #param theFloors for number of floors
* #param theRooms for number of rooms
*/
public boolean roomSearchedAlready(int theFloors, int theRooms) {
boolean searchedRoom;
if (myHidingPlaces[theFloors][theRooms] == ' ') {
myHidingPlaces[theFloors][theRooms] = 'S';
searchedRoom = false;
} else {
searchedRoom = true;
}
return searchedRoom;
}
/**
* Checks if the puppy has been found
*
* #param theFloors for number of floors
* #param theRooms for number of rooms
*/
public boolean puppyLocation(int theFloors, int theRooms) {
if (myHidingPlaces[myFloorLocation][myRoomLocation] == myHidingPlaces[theFloors][theRooms]) {
myFound = true;
} else {
myFound = false;
}
return myFound;
}
/**
* Checks if floors and rooms won't throw out of bounds error
*
* #param theFloors for number of floors
* #param theRooms for number of rooms
*/
public boolean indicesOK(int theFloors, int theRooms) {
boolean indicesFit;
if (theFloors < numberOfFloors() && theRooms < numberOfRooms()) {
indicesFit = true;
} else {
indicesFit = false;
}
return indicesFit;
}
/*
* Checks # of floors and returns it
*/
public int numberOfFloors() {
return myHidingPlaces.length;
}
/*
* Checks # of rooms and returns it
*/
public int numberOfRooms() {
return myHidingPlaces[0].length;
}
/**
* Checks which player found the dog and won, or if not checks to see what player
* guessed wrong and puts their # in the box
*
* #param theFloors for number of floors
* #param theRooms for number of rooms
* #param thePlayer for 1st or 2nd player
*/
public boolean searchRoom(int theFloors, int theRooms, char thePlayer) {
if (myHidingPlaces[myFloorLocation][myRoomLocation] == myHidingPlaces[theFloors][theRooms]) {
myFound = true;
myWinner = thePlayer;
} else {
myHidingPlaces[theFloors][theRooms] = thePlayer;
myFound = false;
}
return myFound;
}
/*
* toString displays the current hidingPlaces array and it’s contents EXCEPT
* the location of the puppy which remains hidden until he/she is found at
* which point toString will be called (by the driver) and both the player
* who found the puppy and a ‘P’ will be displayed in the same cell….
*
*
*
*/
public String toString() {
return null;
}
}
To run this code you'll need to put both codes with their respective posted names and run PuppyPlay.java with LostPuppy.java in the same folder FYI.
The problem lied in this place in PuppyPlay:
found = game.searchRoom(floor, room, players[playerIndex]);
playerIndex = (playerIndex + 1) % 2;
System.out.println("\n[" + floor + "], [" + room + "]");
System.out.println(game.toString());
s.nextLine();
So your program expect you to input something here, and it will keep waiting until you press enter, so you can just remove that line: s.nextLine();
I made a java program that accepts a user input, but when running it in JGrasp after finishing the compile it just says "operation complete" and doesn't let me input anything or display any print text that I added in the code.
/*
* The file name of your program, Assign_1.java
*
* TCSS 143 – January 10th, 2015
* Assignment 1
*/
import java.util.Scanner;
/**
* This program allows 5 town populations to be entered then converted
* to stars based on each 1000.
*
* #author Thejai Riem
* #version 1/10/15
*/
public class Assign_1 {
/**
* For town1-5 variable for population # that changes later
*/
public static int town1;
public static int town2;
public static int town3;
public static int town4;
public static int town5;
public static void main(String[] theArgs) {
}
/**
* Gets population from user input of the 5 towns.
*
* #param theArgs is used for user input to scanner
*/
public static void getPopulation(String[] theArgs) {
Scanner population = new Scanner(System.in); // Keyboard input
System.out.println("Enter the population of town 1: ");
town1 = population.nextInt(); // Asks input for town population
System.out.println("Enter the population of town 2: ");
town2 = population.nextInt(); // Changes town(#) to user input
System.out.println("Enter the population of town 3: ");
town3 = population.nextInt();
System.out.println("Enter the population of town 4: ");
town4 = population.nextInt();
System.out.println("Enter the population of town 5: ");
town5 = population.nextInt();
}
/**
* Draws population one * for 1000 people
*
* #param theArgs is used for string output
*/
public static void drawPopulationBar(String[] theArgs) {
System.out.println();
System.out.println("POPULATION GRAPH:");
System.out.println("Town 1: " + town1 / 1000);
System.out.println("Town 2: " + town2 / 1000);
System.out.println("Town 3: " + town3 / 1000);
System.out.println("Town 4: " + town4 / 1000);
System.out.println("Town 5: " + town5 / 1000);
}
}
The reason it doesn't ask for input is that you're never routing to that section of the code. Note that all Java applications run by calling a main method. Notice that yours is empty. I think what you want is probably something like this:
import java.util.Scanner;
public class Assign_1 {
/**
* For town1-5 variable for population # that changes later
*/
public static int town1;
public static int town2;
public static int town3;
public static int town4;
public static int town5;
public static void main(String[] theArgs) {
getPopulation(theArgs);
drawPopulationBar(theArgs);
}
/**
* Gets population from user input of the 5 towns.
*
* #param theArgs is used for user input to scanner
*/
public static void getPopulation(String[] theArgs) {
Scanner population = new Scanner(System.in); // Keyboard input
System.out.println("Enter the population of town 1: ");
town1 = population.nextInt(); // Asks input for town population
System.out.println("Enter the population of town 2: ");
town2 = population.nextInt(); // Changes town(#) to user input
System.out.println("Enter the population of town 3: ");
town3 = population.nextInt();
System.out.println("Enter the population of town 4: ");
town4 = population.nextInt();
System.out.println("Enter the population of town 5: ");
town5 = population.nextInt();
}
/**
* Draws population one * for 1000 people
*
* #param theArgs is used for string output
*/
public static void drawPopulationBar(String[] theArgs) {
System.out.println();
System.out.println("POPULATION GRAPH:");
System.out.println("Town 1: " + town1 / 1000);
System.out.println("Town 2: " + town2 / 1000);
System.out.println("Town 3: " + town3 / 1000);
System.out.println("Town 4: " + town4 / 1000);
System.out.println("Town 5: " + town5 / 1000);
}
}
I also see that you're not actually using any of the command line arguments (any command line arguments get populated into the string array parameter in the main method invocation). So, you can probably remove those parameters from your other two methods (but you MUST leave it in the main method even if it's not being used since it is part of the signature that java expects).
I think that it's because you didn't fill out the main method of the program.