1 Replies - 282 Views - Last Post: 11 May 2014 - 12:18 PM Rate Topic: -----

#1 spotsy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 02-May 14

associating array with input in Java

Posted 11 May 2014 - 08:39 AM

OK! I have a declared and populated string array containing final tennis scores i.e.{"love", "15", "30", "40", "game"}.
The user has already entered the score as integers 0,1,2,3,4. I need to design a method in a set get class to use the score input in the main class to display the final score in the main.here's what I got:

 import  java.util.*;
public  class  DemoTennisGame
{
	public  static  void  main(String[]  args)
	{
	String player1;
	String player2;
	int score1;
	int score2;

	String finalScore[] = {"love", "15", "30", "40", "game"};

	TennisGame  aTennisGame = new  TennisGame();
	Scanner  keyboard = new Scanner(System.in);

	System.out.print("Enter player 1's name >>  ");
	player1  =  keyboard.nextLine();
	aTennisGame.setPlayer1(player1);
	player1 = aTennisGame.getPlayer1();
	System.out.println("Player 1 is " + player1);

	System.out.print("Enter player 2's name >>  ");
	player2  =  keyboard.nextLine();
	aTennisGame.setPlayer2(player2);
	player2 = aTennisGame.getPlayer2();
	System.out.println("Player 2 is " + player2);

	System.out.print("Enter " + player1 + "'s score >>  ");
	score1  =  keyboard.nextInt();
	aTennisGame.setScore1(score1);
	score1 = aTennisGame.getScore1();
	System.out.print("Enter " + player2 + "'s score >>  ");
	score2  =  keyboard.nextInt();
	aTennisGame.setScore2(score2);
	score2 = aTennisGame.getScore2();
	if(score1 > 4 || score1 < 0 || score2 > 4 || score2 <0) System.out.println("ERROR");
	else
	System.out.println(player1 + " scored " + score1 + " and " + player2 +  " scored " + score2);
	
	//All the above works fine
	
	//need to fill in the code here. I've tried everything I can think of
	System.out.println("Final score is " + finalScore);



	}
}


And here are the class methods. Any help would be greatly appreciated

public class TennisGame
{

	private String player1;
		public  String  getPlayer1()
		{
		return player1;
		}
	public void setPlayer1(String  p1Name)
		{
		 player1 = p1Name;
		}

	private String player2;
		public  String  getPlayer2()
		{
		return player2;
		}
	public void setPlayer2(String  p2Name)
		{
		player2 = p2Name;
		}

	private int score1;
		public  int  getScore1()
		{
		return score1;
		}
		public void setScore1(int  p1Score)
		{
		score1 = p1Score;
		}

	private int score2;
		public  int  getScore2()
		{
		return score2;
		}
		public void setScore2(int  p2Score)
		{
		score2 = p2Score;
		}

	private String finalScore1;
		public  String  getFinalScore1()
		{
		return finalScore1;
		}
		public void setFinalScore1(String  p1Final)
		{
		finalScore1 = p1Final;
		}

	private String finalScore2;

		public  String  getFinalScore2()
	 	{
		return finalScore2;
		}
		public void setFinalScore2(String  p2Final)
		{
		finalScore2 = p2Final;
		}
	}


This post has been edited by modi123_1: 11 May 2014 - 10:18 AM
Reason for edit:: fixed botched code tags


Is This A Good Question/Topic? 0
  • +

Replies To: associating array with input in Java

#2 NormR  Icon User is offline

  • D.I.C Head

Reputation: 27
  • View blog
  • Posts: 54
  • Joined: 25-December 13

Re: associating array with input in Java

Posted 11 May 2014 - 12:18 PM

What problems are you having? Can you post the program's output showing the problem?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1