Java: How to change Class1 variables with Class2 variables - java

So my project is to make a small poker game that will ask users their name, how many players, and your bets.
However before all this, we need to ask the player if they would like to play a NEW game or a SAVED game. The saved game can be hard-coded.
I created a class1 game, and for the saved game file, I created class2. I want class2 to replace the values of class1 but I am not sure how to do this.
The issue is the values of CLASS1 are NOT being replaced by the values of CLASS2 when we call CLASS2. How can I get the variables from CLASS1 to update to CLASS2?
My First Class:
public class NoC2_Musick extends savedGame
{
public static void main(String[] args)
{
savedGame obj = new savedGame();
Scanner input = new Scanner(System.in);
Scanner inputString = new Scanner(System.in);
System.out.println("Is this a NEW Game or a PREVIOUS game?");
System.out.println("How many players will join this game?");
int totalPlayers = input.nextInt();
String[] players = new String[totalPlayers];
obj.oldsavedgame();
System.out.println("What is your name and club?");
for (int i = 0; i < totalPlayers; i++) {
players[i] = inputString.nextLine();
}
System.out.println("These are the Players: ");
for (int i = 0; i < totalPlayers; i++) {
System.out.println(players[i]);
}
System.out.println("Place your Bet.");
int bet = input.nextInt();
}
}
My Second Class:
public class savedGame
{
public int oldsavedgame()
{
int totalPlayers = 3;
String[] players;
players = new String[3];
players[0] = "Sofia";
players[1] = "Shawn";
players[2] = "Tomi";
int bet = 100;
System.out.println("Test");
return totalPlayers;
}
public static void main(String[] args)
{
}
}

I think you are missing some key things here. I do not think you should create a new class for storing the old info.
Just write a method that loads the data into instance variables.
Also, you are not reading the answer to the question: is this a new game / previous.
I would do something along these lines (note: It is not complete, but it should get you headed in the right direction, as far as loading a previous game):
public class NoC2_Musick {
int totalPlayers = 0;
ArrayList<String> players = new ArrayList<String>();
int bet = 0;
private void LoadSavedGame() {
totalPlayers = 3;
players.add("Sofia");
players.add("Shawn");
players.add("Tomi");
bet = 100;
}
public NoC2_Musick() {
Scanner input = new Scanner(System.in);
Scanner inputString = new Scanner(System.in);
System.out.println("Is this a NEW Game? (y/n)");
char newGame = input.next().charAt(0);
if(newGame == 'y')
{
System.out.println("How many players will join this game?");
totalPlayers = input.nextInt();
for (int i = 0; i < totalPlayers; i++) {
System.out.println("What is your name and club?");
players.add(inputString.nextLine());
}
}
else {
LoadSavedGame();
}
System.out.println("These are the Players: ");
for (int i = 0; i < totalPlayers; i++) {
System.out.println(players.get(i));
}
System.out.println("The current bet is: " + bet);
System.out.println("Place your Bet.");
int bet = input.nextInt();
}
public static void main(String[] args) {
new NoC2_Musick();
}
}

Related

How to fix my Array class

I am trying to figure out how to get my array to run correctly, I know I have to change the array value to an input but I cannot get the program to compile if any one can help that be great.
I am trying to have the program take input for grades and names of students and in the end output their name and grade.
Edit sorry this is my first it posting i have an error
Student.java:60: error: class, interface, or enum expected I am in java 101 so this is why it is such low level java, we only know the basics
import java.util.Scanner;
public class students
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many students?: ");
int numofstudents = keyboard.nextInt();
Student s = new Student();
s.setMultipleStudents();
s.toString();
System.out.println("Enter the Grade for the student: ");
int gradeofstudnets = keyboard.nextInt();
}
}
and my second class is
import java.util.Scanner;
public class Student
{
Scanner scan = new Scanner(System.in);
private String name;
private int grade;
private int[] multiplegradeinputs = new int[10];
private String[] multipleStudent = new String[10];
public Student()
{
}
public Student(String n, int g)
{
name = n;
grade = g;
}
public String setMultipleStudents()
{
String n = "";
for(int i = 1; i < multipleStudent.length; i++)
{
System.out.println("Enter student #" + i +" name: " );
n = scan.nextLine();
multipleStudent[i] = n;
}
return null;
}
public String multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
} <--- error here
public String toString()
{
String temp = "";
for(int i = 1; i < multipleStudent.length; i++)
{
temp += multipleStudent[i] + " ";
}
return temp;
}
}
Add return statement in your multiplegradeinputs() method:
public String multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
return null; //Add this line
}
Or change your methods to void return type if they dont return anything.
Class names have to be capitalized in java, so instead of
public class students
you should write
public class Students
Also instead of writing
keyboard.nextInt();
You should write
Integer.parseInt(keyboard.nextLine());
This is mainly because java is full of bugs and technical specifications that you won't find easily. Let me know if this fixes it for you, since you didn't post the exact error message you got.
As for the error that you pointed out, it's because your function expects a String as a return value no matter what, so either change that to void if you can or return a null string. To do that just add the following line at the very end of the method.
return null;
You should create a Student object which holds the properties of the student, e.g. Name and Grades. You should then store all the student objects in some kind of data structure such as an array list in the students class.
Adding to the answer provided by #hitz
You have a bug in the for loops:
for(int i = 1; i <multiplegradeinputs.length; i++)
for(int i = 1; i < multipleStudent.length; i++)
You will never populated multiplegradeinputs[0] and multipleStudent[0] because you start the loop at index == 1 and thus you will have only 9 student names stored instead of 10.
Change to:
for(int i = 0; i <multiplegradeinputs.length; i++)
for(int i = 0; i < multipleStudent.length; i++)
Remember even though the length in 10, the indices always start with 0 in Java and in your case will end with 9.
import java.util.Scanner;
public class Student
{
Scanner scan = new Scanner(System.in);
private String name;
private int grade;
private int[] multiplegradeinputs = new int[10];
private String[] multipleStudent = new String[10];
public Student()
{
}
public Student(String n, int g)
{
name = n;
grade = g;
}
public String setMultipleStudents()
{
String n = "";
for(int i = 1; i < multipleStudent.length; i++)
{
System.out.println("Enter student #" + i +" name: " );
n = scan.nextLine();
multipleStudent[i] = n;
}
return null;
}
public void multiplegradeinputs()
{
for(int i = 1; i <multiplegradeinputs.length; i++)
{
System.out.println("Enter the Grade of the student#" + i +" : ");
grade = scan.nextInt();
multiplegradeinputs[i] = grade;
}
}
public String toString()
{
String temp = "";
for(int i = 1; i < multipleStudent.length; i++)
{
temp += multipleStudent[i] + " ";
}
return temp;
}
}
this is the 2nd class
import java.util.Scanner;
public class students
{
public static void main(String[] args)
{
Scanner keyboard = new Scanner(System.in);
System.out.println("How many students?: ");
int numofstudents = keyboard.nextInt();
Student s = new Student();
s.setMultipleStudents();
s.toString();
System.out.println("Enter the Grade for the student: ");
int gradeofstudnets = keyboard.nextInt();
}
}
You are missing a return value in the multiplegradeinputs() method.

Storing info into an array of another class

I've been working on this for a while but can't seem to get it. I need to store user input into an array from another object but I can't get it to work. I'm not sure if its my constructor or I'm missing something but any help is appreciated
Here is the output program
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'p' to create a polygon");
String in = sc.next();
if (in.equals("p")) {
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side=0;
Polygon ps;
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
ps = new Polygon(side);
}
ps = new Polygon(side);
Here is the constructor of the other class
public class Polygon {
protected int[] sideLengths;
public Polygon(int sides){
sideLengths= new int[sides];
}
you need to do
ps=new Polygon(numSides+1); //adding 1 because for-loop starts with index 1
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
ps[i]=side;
}
by this what we do is assign the 'side' value entered by user to a particular index in array.
hope this helps!
Good luck
The way I understand your Program I think you are looking for this kind of logic
if (in.equals("p")) {
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side = 0;
Polygon ps;
int sideLengths[] = new int[numSides];// array to store length of polygon of sides `numSides`
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
sideLengths[i-1] = sc.nextInt();// creating array to store lengths
}
ps = new Polygon(sideLengths);// generating your pollygon by sending the array
}
And you Polygon class should look like this
class Polygon {
protected int[] sideLengths;// storing the lengths of all sides
public Polygon(int dimensions[]) {
sideLengths = dimensions ;
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'p' to create a polygon");
String in = sc.next();
if(in.equalsIgnoreCase("p")) {
Integer sides[];
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side=0;
if(numSides>0){
sides = new Integer[numSides];
}
Polygon ps;
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
sides[i] = side;
}
ps = new Polygon(sides);
}
}
public class Polygon {
private Integer[] sides;
public Polygon(Integer[] sides){
this.sides = sides;
}
}
Now we have created an array of size of sides and array is been passed as an argument to constructor for initializing Polygon's object
it should be like
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter 'p' to create a polygon");
String in = sc.next();
if (in.equals("p")) {
System.out.println("How many sides?");
int numSides = sc.nextInt();
int side=0;
Polygon ps = new Polygon(numSides);
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
ps.addSide(side, i-1);
//ps = new Polygon(side);
}
//ps = new Polygon(side);
}}}
and Polygon class like
public class Polygon {
protected int[] sideLengths;
public Polygon(int sides){
sideLengths= new int[sides];
}
public void addSide(int side, int index){
sideLengths[index] = side;
}
}
You are calling the constructor correctly, but after for loop ends you are again creating a new instance of Polygon and assigning it to ps. Either create an array of int or ArrayList of Integer, and store each instance of Polygon side length in that just before the for loop ends.
ArrayList<int> psListpsSideLengthList = new ArrayList<int>;
for (int i = 1; i <= numSides; i++) {
System.out.println("Enter the length of side " + i);
side = sc.nextInt();
psListpsSideLengthList.add(side);
}

Methods' outputting twice with arrays

I have to basically make a program that would add a name given a prompt and place it in the array. It should do the same for the age of the person. I have been forced to do this with methods. The only issue i am having is that with the 3rd last line, the name get's asked twice. I don't know how to fix that. Any help is appreciated.
public class Testing1 {
public static int[] ageinput(String names[], int q2){
int holderage[] = new int[q2];
for(int x = 0; x<q2;x++) {``
System.out.println("Please input the age of " + names[x]);
Scanner age = new Scanner(System.in);
int a1 = age.nextInt();
holderage[x] = a1;
}
return holderage;
}
public static String[] nameinput(int q2){
String holdername[] = new String[q2];
for (int x = 0; x<q2;x++) {
System.out.println("Please input the name of the person");
Scanner name = new Scanner(System.in);
String n1 = name.nextLine();
holdername[x]=n1;
}
return holdername;
}
public static void output(String names[], int ages[]){
for(int x = 0; x<names.length;x++){
System.out.println(names[x]+" is "+ages[x]+" years old");
}
}
public static void main(String[] args) {
System.out.println("How many names do you want to input?");
Scanner question = new Scanner(System.in);
int q1 = question.nextInt();
output(nameinput(q1),ageinput(nameinput(q1),q1));
}
}
In output(nameinput(q1),ageinput(nameinput(q1),q1)); you call the method nameinput twice, so the code also will be executed twice.
You could ask the names in nameinput, store them into an array and pass that array to ageinput.
#Aadithya Gowthaman, try this one in your editor.
package com.aamir;
import java.util.Scanner;
public class Testing1 {
public static int[] ageinput(String names[], int q2){
int holderage[] = new int[q2];
for(int x = 0; x<q2;x++) {
System.out.println("Please input the age of " + names[x]);
Scanner age = new Scanner(System.in);
int a1 = age.nextInt();
holderage[x] = a1;
}
return holderage;
}
public static String[] nameinput(int q2){
String holdername[] = new String[q2];
for (int x = 0; x<q2;x++) {
System.out.println("Please input the name of the person");
Scanner name = new Scanner(System.in);
String n1 = name.nextLine();
holdername[x]=n1;
}
return holdername;
}
public static void output(String names[], int ages[]){
for(int x = 0; x<names.length;x++){
System.out.println(names[x]+" is "+ages[x]+" years old");
}
}
public static void main(String[] args) {
System.out.println("How many names do you want to input?");
Scanner question = new Scanner(System.in);
int q1 = question.nextInt();
String [] namesArray = nameinput(q1);
int [] ageArray = ageinput(namesArray, q1);
output(namesArray, ageArray);
}
}

Java: The constructor gerbil(int) is undefined

So I've read over all of the constructor undefined posts on stackoverflow and tried the solutions and they haven't worked for me. Maybe I'm trying it wrong. I keep getting "the constructor Gerbil(int) is undefined."
The code that's the problem:
GerbilArray[i] = new Gerbil(i);
My full code:
import java.util.Scanner;
public class Gerbil {
public String name;
public String id;
public String bite;
public String escape;
public Gerbil() {
this.name = "";
this.id = "";
this.bite = "";
this.escape = "";
}
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("How many foods?");
int totalFood = keyboard.nextInt();
System.out.println("How many gerbils in the lab?");
int numberOfGerbils = keyboard.nextInt();
Gerbil[] GerbilArray = new Gerbil[numberOfGerbils];
for(int i = 0; i <= numberOfGerbils; i++){
GerbilArray[i] = new Gerbil(i);
System.out.print("Lab ID:");
String id = keyboard.next();
System.out.print("Gerbil Nickname:");
String name = keyboard.next();
System.out.print("Bite?");
String bite = keyboard.next();
System.out.print("Escapes?");
String city = keyboard.nextLine();
for (int j = 0; j < totalFood; j++) {
System.out.println("How many of food " + (j+1) + "do you eat?:");
}
}
}
}
Also you've probably seen that my nested for-loop isn't finished as well. I'm trying to make an array inside of an object that will store "x" amount of integers inside of my object listed from the user (int totalFood) but I have no idea how.
You don't have a constructor Gerbil(int a) in the class Gerbil and you try to call it!
Just call it this way:
GerbilArray[i] = new Gerbil();

Creating an array of arrays in Java

Say I have a Player class such as:
public class Player {
String name;
int chips;
int betVal;
}
Is the following code correct for creating the array of the players?
public static void main(String[] args) {
int playerCount;
int startingChip;
out.print("How many players? ");
playerCount = myScanner.nextInt();
Player[] aPlayer = new Player[playerCount + 1];
for (int i = 0; i < playerCount + 1; i++){
aPlayer[i] = new Player();
}
out.print("Enter starting chip amount: ");
startingChip = myScanner.nextInt();
}
If so, how would I assign the name, chip amount and the betVal to each player? How would I access and alter them later on in the code?
EDIT: Will it be easier leaving the Player as an object or an array (name,chips,betVal) for accessing it later on?
First off, your variables need to be private (there's a section of programmers who prefer public variables but most prefer private).
You could set the values of the Player object either through an overloaded constructor or through the setters.
public class Player {
private String name;
private int chips;
private int betVal;
public Player(){
//default constructor to initialize without any parameters
}
public Player(String name, int chips, int betVal){
this.name=name;
this.chips=chips;
this.betVal=betVal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getChips() {
return chips;
}
public void setChips(int chips) {
this.chips = chips;
}
public int getBetVal() {
return betVal;
}
public void setBetVal(int betVal) {
this.betVal = betVal;
}
}
In which case, your initialization could be either
aPlayer[i] = new Player("Jason", 5,1000);
or using the setters as in
for (int i = 0; i < playerCount + 1; i++){
aPlayer[i] = new Player();
}
aPlayer[i].setName("Jason");
aPlayer[i].setChips(5);
aPlayer[i].setBetVal(1000);
Considering your sample program, I guess option 2 plays well.
You can access each player by telling the array which one you want to access.
`aPlayer[0].name = "JAG";`
would work for example.
Why not ask all the user's inputs before creating anything ? For exemple :
public static void main(String[] args) {
int playerCount;
int startingChip;
out.print("How many players? ");
playerCount = myScanner.nextInt();
out.print("Enter starting chip amount: ");
startingChip = myScanner.nextInt();
Player[] aPlayer = new Player[playerCount];
for (int i = 0; i < playerCount; i++){
aPlayer[i] = new Player();
aPlayer[i].setChips(startingChip);
}
}
You should also use a List. And finally, if you want to ask for the name of each player, do it directly in the loop :
for (int i = 0; i < playerCount; i++){
aPlayer[i] = new Player();
aPlayer[i].setChips(startingChip);
out.print("What's the player " + i + " name? ");
aPlayer[i].setName(myScanner.next());
}
try something like this...
ask for individual players info in the loop itself.
System.out.println("How many players? ");
Scanner myScanner = new Scanner(System.in);;
playerCount = myScanner.nextInt();
Player[] aPlayer = new Player[playerCount];
for (int i = 0; i < playerCount; i++){
aPlayer[i] = new Player();
System.out.println("Enter Name for Player " + i+1);
String name = myScanner.next();
System.out.println("Enter chips for Player " + i+1);
int chips = myScanner.nextInt();
System.out.println("Enter betVal for Player " + i+1);
int betVal = myScanner.nextInt();
aPlayer[i].name = name;
aPlayer[i].chips = chips;
aPlayer[i].betVal = betVal;
}
Also make the instance variables of Player class private, and access them using getters and setters.

Categories

Resources