Learned people of the internet. I'm currently working on an assignment for a first year programming class and, to be honest, programming is not my greatest skill. So any help with this small problem would be much appreciated.
The program is essentially a number guessing game, that stores and sorts the guesses into an arraylist and displays, once you have guessed correctly, the average, the lowest guess, the index array of the correct guess, the average and the converted temperature in Fahrenheit. It is between 9 and 40 because the assignment specified 40 and the highest number on my student ID. Also the reason for the unnecessary amount of classes when I could simply put them into one is also the assignment specifications fault.
The trouble I am having is storing the temperature guesses, whether correct or incorrect, in the arraylist. I believe I have initialised the arraylist correctly within my constructor but I am not sure how or where to capture the guesses from the jTextField. Once I figure that out I can incorporate the other methods I have written for the displayed results and be done with this assignment.
Once again, I apologise for my general newbiness and any help would be extremely appreciated.
package TemperatureGuessApp;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import javax.swing.*;
public class TemperatureFrame extends JFrame {
public JFrame mainFrame;
public JLabel prompt1, prompt2;
public JTextField userInput;
public JLabel comment;
public JButton restart;
public int randomTemperature;
public int min = 9;
public int max = 40;
public int guessTemperature;
public int sumTemperature;
public double avgTemperature;
public int lowestTemperature;
public int convertedTemperature;
public int indexTemperature;
public Color background;
public TemperatureFrame() {
super("Temperature Guess/Conversion Application");
prompt1 = new JLabel("Randomly generated temperature is between 9 and 40.");
prompt2 = new JLabel("Write temperature (your guess) or -1 (for exit) and press enter key:");
userInput = new JTextField(5);
userInput.addActionListener(new GuessHandler());
ArrayList<Integer> guesses = new ArrayList<>();
comment = new JLabel("The results will be shown here.");
restart = new JButton("Start Again - Generate A New Temperature");
restart.addActionListener(
new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
userInput.setText("");
comment.setText("The results will be shown here.");
RandomTemperature();
userInput.setEditable(true);
}
});
setLayout(new FlowLayout());
background = Color.LIGHT_GRAY;
add(prompt1);
add(prompt2);
add(userInput);
add(comment);
add(restart);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 150);
setLocationRelativeTo(null);
setVisible(true);
setResizable(false);
RandomTemperature();
ConvertTemperature();
}
public void RandomTemperature() {
Random random = new Random();
randomTemperature = random.nextInt(max - min) + min;
}
public void SortTemperature() {
}
public void FindLowestTemperature() {
}
public void FindAverageTemperature() {
}
public void FindIndexTemperature() {
}
public void ConvertTemperature() {
convertedTemperature = randomTemperature * 9 / 5 + 32;
}
class GuessHandler implements ActionListener {
@ Override
public void actionPerformed(ActionEvent e) {
{
guessTemperature = Integer.parseInt(userInput.getText());
{
if (guessTemperature > randomTemperature) {
comment.setText("Temperature guessed is higher than the random temperature.");
userInput.setText("");
userInput.setEditable(true);
}
if (guessTemperature < randomTemperature) {
comment.setText("Temperature guessed is lower than the random temperature.");
userInput.setText("");
userInput.setEditable(true);
}
if (guessTemperature == randomTemperature) {
comment.setText("Temperature guessed is equal to the random temperature.");
JOptionPane.showMessageDialog(null, "Temperature guessed is equal to the random temperature.\n\n1. Lowest temperature is:" + lowestTemperature + "\n2. Average temperature is:" + avgTemperature + "\n3. Array index of correctly guessed temperture is:" + indexTemperature + "\n4. Temperature in Farenheit is:" + convertedTemperature + "\n\nThank you for playing!");
}
else if (guessTemperature == -1) {
System.exit(0);
}
}
}
}
}
}
1. First create a Pojo
to store all the data of players...
Eg
public class Player{
int prompt1;
int prompt2;
int userInput;
String comment;
int restart;
}
2. Then store them in ArrayList
....
//////////////////////////Edited///////////////////////////////
public class Player{
int prompt1;
int prompt2;
int userInput;
String comment;
int restart;
public Player(int p1, int p2, int usInput, String c, int res){
this.prompt1 = p1;
this.prompt2 = p2;
this.userInput = usInput;
this.comment = c;
this.restart = res ;
}
}
public class Test{
ArrayList<Player> arList = new ArrayList<Player>();
arList(new Player(p1, p2, usInput, c, res)); // These Arguments u must declare and initialize....
}
///////////////////////Edited///////////////////////////
ok fine... Adding of int
guessTemperature into the ArrayList
from JTextField
WITHOUT USING POJO , can be done in this way.
ArrayList<Integer> arList = new ArrayList<Integer>();
String str = jText.getText().toString();
int gTemp = Integer.parseInt(str);
arList.add(gTemp);
Now Suppose you have this Player with 4 attributes, like TemperatureInput, Name, Age, Score... etc... And N nos of players are playing this game..then you should be using Pojo and Collection (ie.. List, Set, Map)
public class Player{
int guessTemp;
String name;
int age;
long score;
public Player(int gTemp, int name, int age, int score){
this.guessTemp = gTemp;
this.name = name;
this.age = age;
this.score = score;
}
public int getGuessTemp() {
return guessTemp;
}
public void setGuessTemp(int guessTemp) {
this.guessTemp = guessTemp;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public long getScore() {
return score;
}
public void setScore(long score) {
this.score = score;
}
}
Now... from another class it should be like this....
public class Test{
public static void main(String[] args){
ArrayList<Player> pList = new ArrayList<Player>();
String name = jName.getText().toString();
long score = Long.parseLong(jScore.getText().toString());
int age = Integer.parseInt(jAge.getText().toString());
int gTemp = Integer.parseInt(jTemp.getText().toString());
pList.add(new Player(gTemp, name, age, score));
}
}