6 Replies - 678 Views - Last Post: 13 October 2010 - 04:11 PM Rate Topic: -----

#1 pockypirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-October 10

[Homework Help] General Overview of Classes

Posted 13 October 2010 - 10:44 AM

Hello everyone.

I'm in one of my first of many Java classes and we've just started working with writing classes. I understand the concept more or less, but I'm having trouble understand how to actually write them. In particular, we're working on an in class mini-game that asks to create a Player class to store scores; a name, username, number of games played, wins and losses. All which must be written to a file per username and updated with each play of the game.

We're asked to them write methods for:

a)Login this will test a player's Id against a file (whose name is passed into the method as a parameter). If the id is found in the file the player's logged status will be marked as "true"; otherwise it will be marked as "false".

b)Register this will test a player's Id against a file (whose name is passed into the method as a parameter). If the Id is not found in the file it will be added to the end of the file and the player's logged status will be marked as "true"; otherwise it will be marked as "false".

c)Update his/her wins and update his/her losses (since the main program will not be able to access the instance variables directly) - This is where I'm iffy on the use of files within methods and classes. Do I just write a method that says hey, open this file, read in the previous number recorded for numWins and the like, increment and rewrite?

d)Return a formatted String version of the player's information for output - I think I've got this with the return methods

e)Write the player's information back to the appropriate file (based on the player's Id).

I'm honestly not sure how to get these classes working and was wondering if someone might be able to summarize classes in a way that better explains them than my textbook.

I have a variable in my main program called numWins, etc. Do I even need that there if I have a variable numWins and the like in the Player class? And I'm particularly having trouble writing score back to the file. I would open the user's file first, read in the previous values of the variables then use FileWriter to append, but can I do this all through a class method? I'm guessing I'd then need a method to set the scores at 0 for a new player as well.

Here's what I have so far, which is basically a skeleton of sorts...

public class Player
{
	private boolean loginStatus;
	private int numWins;
	private int numLosses;
	private int gamesPlayed;
	private String firstName;
	private String lastName;
	private String userName;
	
	public  setStats(int numWins, int numLosses, int gamesPlayed)
	{
		//initializes stats for a new player to the first game
		//not sure if I'm doing this part right
		wins = numWins ;
		losses = numLosses;
		totalPlayed = gamesPlayed;
		
	}
	
	public void updateWins();
	{
		//if player wins, update and rewrites score
		//open userName.txt
		//I still need to keep the username and Full Name though, not append, but not really rewrite
		PrintWriter outputFile = new PrintWriter(fwriter);
		
		//read num of wins already
		outputFile.println(numWins++);
		outputFile.println(gamesPlayed++);
		outputFile.close();
		//increment by 1
		//rewrite
		//close file
	}
	
	public void updateLosses();
	{
		//if player losses, update and rewrites score
		//open userName.txt
		PrintWriter outputFile = new PrintWriter(fwriter);
		
		//read num of losses already
		outputFile.println(numLosses++);
		outputFile.println(gamesPlayed++);
		outputFile.close();
		//increment by 1
		//rewrite	
	}
	
	public int getWins()
	{
		return numWins;
	}
	
	public int getLosses()
	{
		return numLosses;
	}
	
	public int getGamesPlayed()
	{
		return gamesPlayed;
	}
	
	public String getID()
	{
		return userName;
	}
	
	public String getName()
	{
		return firstName + lastName;
	}
	
	public void displayStats();
	{
		System.out.println("Username: " + userName"\nName: " firstName +" " lastName"\nTotal Games Played: " + gamesPlayed"\nGames Won: " + 
							numWins"\nGames Lost: " + numLosses);
	}
	

}


I'm honestly just lost on writing classes and any help would be appreciated. Thanks guys.

Is This A Good Question/Topic? 0
  • +

Replies To: [Homework Help] General Overview of Classes

#2 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1820
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: [Homework Help] General Overview of Classes

Posted 13 October 2010 - 11:23 AM

I am not good in Java but I think you can go to tutorial part where you can get information about classes and game programming.
Was This Post Helpful? 1
  • +
  • -

#3 na253487  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 13-October 10

Re: [Homework Help] General Overview of Classes

Posted 13 October 2010 - 12:05 PM

Well, I am in no way an authority like some people on this site are. I've been browsing here for a while and finally decided to contribute

Your overall structure really isn't that bad, just a few things that I can point out.

First thing, I think that if you need to read from a .txt file and write to it with updated information you need to utilize FileInputStream and FileOutputStream. You can check the Java API to get a full understanding of these methods as I cannot provide a coherent one. I can say that when I have had to read from a .txt file before, I used FileInputStream and it pulls from the file into your class. FileOutputStream should do the opposite.

I believe you need some setters to go with your getters. You are getting the information and storing it, but you should be setting it, and then outputting it back to the .txt file on a database.

Also, this is pure convention, but when naming your getters and setters, you should stick to using the names of your variables, like so:

public int getNumWins() {
    return numWins;
}

public void setNumWins(int numWins) {
    this.numWins = numWins;
}



Last thing. If you are going to create method, especially one that takes number of wins, losses, etc. as parameters, it will probably be a good idea to call it later in your code. So in your case, I'd make it a class constructor so you can easily use it.

pubilc Player(int numWins, int numLosses, int gamesPlayed) {
    wins = numWins;
    losses = numLosses;
    games = gamesPlayed;
}



Then when you wanted to call that class with those parameters later:

//something close to this
Player player = new Player(6, 4, 10);



I hope this helps... Like I said someone else will give you great advice, I just helped out where I could. Good luck.
Was This Post Helpful? 1
  • +
  • -

#4 na253487  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 13-October 10

Re: [Homework Help] General Overview of Classes

Posted 13 October 2010 - 12:18 PM

EDIT: I found some code I had using FileInputStream.

public static void main(String[] args) throws FileNotFoundException {
    //Properties is part of the Java API. I don't think you will need this if your file is a .txt
    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("name_of_your_file.properties");
	//this is a try-catch block that handles exceptions and will load the FileInputStream	
	try {
	    prop.load(fis);
	}
	catch (IOException e) {
	}
}


Was This Post Helpful? 0
  • +
  • -

#5 pockypirate  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 13-October 10

Re: [Homework Help] General Overview of Classes

Posted 13 October 2010 - 12:29 PM

View Postna253487, on 13 October 2010 - 11:18 AM, said:

EDIT: I found some code I had using FileInputStream.

public static void main(String[] args) throws FileNotFoundException {
    //Properties is part of the Java API. I don't think you will need this if your file is a .txt
    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("name_of_your_file.properties");
	//this is a try-catch block that handles exceptions and will load the FileInputStream	
	try {
	    prop.load(fis);
	}
	catch (IOException e) {
	}
}



FileInput/OutputStream is different then from the PrintWriter and FileWriter then? I'm definitely up for playing around with new things, but I know our book has had us using PrintWriter and FileWriter. I'm not picky though.

And thank you -- that was really helpful!
Was This Post Helpful? 0
  • +
  • -

#6 na253487  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 13-October 10

Re: [Homework Help] General Overview of Classes

Posted 13 October 2010 - 01:01 PM

View Postpockypirate, on 13 October 2010 - 11:29 AM, said:

View Postna253487, on 13 October 2010 - 11:18 AM, said:

EDIT: I found some code I had using FileInputStream.

public static void main(String[] args) throws FileNotFoundException {
    //Properties is part of the Java API. I don't think you will need this if your file is a .txt
    Properties prop = new Properties();
    FileInputStream fis = new FileInputStream("name_of_your_file.properties");
	//this is a try-catch block that handles exceptions and will load the FileInputStream	
	try {
	    prop.load(fis);
	}
	catch (IOException e) {
	}
}



FileInput/OutputStream is different then from the PrintWriter and FileWriter then? I'm definitely up for playing around with new things, but I know our book has had us using PrintWriter and FileWriter. I'm not picky though.

And thank you -- that was really helpful!




If your book explicitly points out to use PrintWriter and FileWriter... go for it. I'm not familiar with those.

I did a quick check for PrintWriter and I've seen it used in conjunction with FileInput/OutputStream. So maybe someone can clarify.

One thing I did see for sure is that PrintWrite never throws IO Exceptions. So if there is an error in reading/writing the data, you won't know. So what you need to most likely do is use the checkError() method associated with it.
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10767
  • View blog
  • Posts: 40,093
  • Joined: 27-December 08

Re: [Homework Help] General Overview of Classes

Posted 13 October 2010 - 04:11 PM

PrintWriter is something we are all familiar with, even if we don't know it, as System.out (as in System.out.println()) is a PrintWriter. The FileWriter class is for writing character streams to a File, and allows you to append to the File without starting over.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1