Quantcast

Jump to content


Photo

Java assignment review.


  • Please log in to reply
24 replies to this topic

#1 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 22 November 2011 - 12:39 PM

Howdy,

I typed up this class, and just want to get some feedback and some help from you guys. I'll post the problem, and if you guys don't mind critiquing I would appreciate it. Also, I'm a little confused on how the interface of this will work when executed in a main class.

Design a class that keeps track of a student’s food purchases at the campus cafeteria. A meal card is assigned to an individual student. When a meal card is first issued, the balance is set to the number of points. If the student does not specify the number of points, then the initial balance is set to 100 points. Points assigned to each food item are a whole number. A student can purchase additional points at any time during a semester. Every time food items are bought, points are deducted from the balance. If the balance becomes negative, the purchase of food items is not allowed. There is obviously more than one way to implement the MealCard class. Any design that supports the key functionalities is acceptable. Put this class in the myutil package.





public class MealCard {

	

	private static int balance;

	private final static int START_BALANCE = 100;

	

	

	public MealCard(){

		this(balance = START_BALANCE);

	}

	public MealCard(int bal) {

		balance = bal;

	}

	

	

	public int deductBalance(int cost){

		if((balance-cost) >= 0){

			balance -= cost;

		}

		else if((balance - cost) < 0){

			System.out.println("Insufficient Funds.");

		}

		return balance;

			

	}

	

	public int buyPoints(int amt){

		return balance += amt;

		

	}





}


#2 Inkheart

Inkheart
  • 268 posts

Posted 22 November 2011 - 12:46 PM

I can't really comment on Java in particular, but

if((balance-cost) >= 0)

could just be

if (cost <= balance)

Also, you really don't need an "else if" if there are only two scenarios; an "else" suffices. Minor tidbits, but it helps to get into the habit of writing clear and concise code early on.

#3 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 22 November 2011 - 12:52 PM

I took the "if" out of "else if". Is that what you meant? Because when I did that it gave me a syntax error.

#4 Inkheart

Inkheart
  • 268 posts

Posted 22 November 2011 - 01:02 PM

I took the "if" out of "else if". Is that what you meant? Because when I did that it gave me a syntax error.


You can kill the entire thing. That is, replace

else if((balance - cost) < 0){

with just

else {

Edited by Inkheart, 22 November 2011 - 01:02 PM.


#5 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 22 November 2011 - 01:09 PM

Oh. Well, now it makes sense. I thought about doing it as a boolean expression, but I didn't know how, so I just felt more comfortable with the if statement. See anything else?

#6 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 22 November 2011 - 04:30 PM

Why don't you just initialize the balance variable to the start balance from the get-go rather than having a bunch of constructors that end up doing the same.

#7 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 22 November 2011 - 06:52 PM

If the user doesn't choose a start balance, then it is automatically 100. If they do choose a start balance, the balance is whatever the choose. I thought I would have to have overloaded constructors to do that.

#8 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 22 November 2011 - 10:54 PM

If the user doesn't choose a start balance, then it is automatically 100. If they do choose a start balance, the balance is whatever the choose. I thought I would have to have overloaded constructors to do that.

Oh, I mean you can just do this:

public class Test {
    private int startBalance = 100;

    public Test() {
        // do some constructor business here
    }

    public Test(int startBalance) {
        this.startBalance = startBalance;
    }
}

That way you can still have a default constructor, along with a constructor that takes in a start balance, and it will all just do the right thing.

#9 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 22 November 2011 - 11:18 PM

Oh, I mean you can just do this:

public class Test {
    private int startBalance = 100;

    public Test() {
        // do some constructor business here
    }

    public Test(int startBalance) {
        this.startBalance = startBalance;
    }
}

That way you can still have a default constructor, along with a constructor that takes in a start balance, and it will all just do the right thing.


Isn't that what he was already doing?

#10 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 23 November 2011 - 01:15 AM

Isn't that what he was already doing?


Yeah but the additional variable and line of code in the default constructor is not necessary. It doesn't hurt, just not needed.

#11 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 23 November 2011 - 01:39 AM

Yeah but the additional variable and line of code in the default constructor is not necessary. It doesn't hurt, just not needed.


Oh I see. But does java let you initialize member variables outside of methods? I mean, the class needs to somehow set that member to 100 by default. In C++ you'd do it in the constructor the way he's doing.

#12 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 23 November 2011 - 02:37 AM

Oh I see. But does java let you initialize member variables outside of methods? I mean, the class needs to somehow set that member to 100 by default. In C++ you'd do it in the constructor the way he's doing.


Yep :) It makes much more sense too, as you don't need to repeat code if you're using multiple constructors.

#13 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 23 November 2011 - 08:11 AM

Oh, I mean you can just do this:

public class Test {
    private int startBalance = 100;

    public Test() {
        // do some constructor business here
    }

    public Test(int startBalance) {
        this.startBalance = startBalance;
    }
}

That way you can still have a default constructor, along with a constructor that takes in a start balance, and it will all just do the right thing.


So, in order to make the first default constructor set the balance to 100, I would have to place startBalance in their somewhere?

#14 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 23 November 2011 - 10:10 AM

So, in order to make the first default constructor set the balance to 100, I would have to place startBalance in their somewhere?

The startBalance is set to 100 before the default constructor is even called. It is initialized to 100 on its declaration. It's a bit of a quirk, but there can be execution before the constructor is called.

This is really not a big deal though, so don't worry about it too much. Just a suggestion.

#15 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 23 November 2011 - 11:37 AM

The startBalance is set to 100 before the default constructor is even called. It is initialized to 100 on its declaration. It's a bit of a quirk, but there can be execution before the constructor is called.

This is really not a big deal though, so don't worry about it too much. Just a suggestion.


I would rather learn to program efficiently the first time around, than come back later and learn how to. Posted Image

I'm still trying to think of how to create a main class that efficiently uses this class as well.

#16 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 23 November 2011 - 11:46 AM

Yep :) It makes much more sense too, as you don't need to repeat code if you're using multiple constructors.


Hmm, TIL - that's pretty cool, C++ doesn't allow it.

#17 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 23 November 2011 - 12:21 PM

Hmm, TIL - that's pretty cool, C++ doesn't allow it.

Actually, it does, but in a very limited sense. You can initialize static const integral types, like so:

class Foo {
    public:
        static const int bar = 1;
};

Again, it's not a big deal. While it is good to know the ins and outs of the language you are using, some features are archaic or unnecessary. C++ is one of those languages that tries to allow programmers to program in whichever paradigm they like. It's made the language large and bloated with many of the programmers who program in it using just a subset of the language.

Digraphs and trigraphs, anyone?

#18 Melchoire

Melchoire
  • 5284 posts


Users Awards

Posted 23 November 2011 - 12:29 PM

Actually, it does, but in a very limited sense. You can initialize static const integral types, like so:

class Foo {
    public:
        static const int bar = 1;
};

Again, it's not a big deal. While it is good to know the ins and outs of the language you are using, some features are archaic or unnecessary. C++ is one of those languages that tries to allow programmers to program in whichever paradigm they like. It's made the language large and bloated with many of the programmers who program in it using just a subset of the language.

Digraphs and trigraphs, anyone?


That's pretty neat. And it pretty serves the same purpose as what the java class does.

Doesn't let you initialize arrays in that way though. Or is there a way around that too?

#19 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 23 November 2011 - 12:59 PM

That's pretty neat. And it pretty serves the same purpose as what the java class does.

Doesn't let you initialize arrays in that way though. Or is there a way around that too?

I believe it's just integral types (integers, doubles, floats, etc). You cannot allocate memory like this so pointers (pointers to memory -- arrays) can't be allocated like this. If you really wanted to, you could create a class to encapsulate an array and declare it to be allocated on the stack. In fact, this is exactly what std::vector does.

#20 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 24 November 2011 - 07:21 AM

public class Test {
    private int startBalance = 100;

    public Test() {
        // do some constructor business here
    }

    public Test(int startBalance) {
        this.startBalance = startBalance;
    }
}



Honestly, I'm still confused by this. What do you mean "do some constructor business"? I'm getting errors now for all my "balance" methods since I took it out. Which is easy enough to change, but I'm still confused as to what to do with the default constructor.

I ended up putting

public class MealCard {

    private static int startBalance = 100;

    

    public MealCard() {

    	this(startBalance);

    }




    public MealCard(int startBalance) {

        this.startBalance = startBalance;


    
}


But how do I use startBalance in my methods if it is private?

Edited by SirBaggy, 24 November 2011 - 07:28 AM.


#21 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 24 November 2011 - 08:17 AM

public class Test {
    private int startBalance = 100;

    public Test() {
        // do some constructor business here
    }

    public Test(int startBalance) {
        this.startBalance = startBalance;
    }
}



Honestly, I'm still confused by this. What do you mean "do some constructor business"? I'm getting errors now for all my "balance" methods since I took it out. Which is easy enough to change, but I'm still confused as to what to do with the default constructor.

I ended up putting

public class MealCard {

    private static int startBalance = 100;

    

    public MealCard() {

    	this(startBalance);

    }




    public MealCard(int startBalance) {

        this.startBalance = startBalance;


    
}


But how do I use startBalance in my methods if it is private?


You need to understand the scope of variable decelerations. If you create a variable inside a method, it can only be used within that method. If you create it within the class itself as you have above, the entire class can see it no matter what. The "private" actually refers to how it's handled outside of that class:

  • Public - This variable can be accessed directly from other classes and sub-classes from any package.
  • Protected - This variable can only be accessed from other classes within the same package as your class and sub-classes of your class.
  • Default (don't type anything) - This variable can only be accessed by classes within the same package as your class.
  • Private - The variable can only be used within this class.
Try to think of the code like this. If you tell your MealCard class what you want the balance to be, you want to set it to that value, right? But if you don't tell it, you want it to be 100.
When you declare your balance, you set it to 100. This means if someone creates your class without passing a balance, you don't have to do anything because you already set it to 100 earlier. You only want to change it in the other constructor.

public class Test {
    private int startBalance = 100;

    public Test() {
        // do some constructor business here
    }

    public Test(int startBalance) {
        this.startBalance = startBalance;
    }
}

If I did:

Test t = new Test();

then I've just created a new Test object, and the balance is set to 100 because the default constructor has no code in to change it to something else. You don't have to do any "constructor business", that was just optional.

Additionally, if I said:

Test t = new Test(200);

this time you actually have code in the constructor to change the balance.

#22 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 24 November 2011 - 09:17 AM

Wow, Sida. That was awesome, ha. I get it now. I finished my class, with no errors yet. Here it is, so you guys can see it.



public class MealCard {

    private int startBalance = 100;

    

    public MealCard() {

    }




    public MealCard(int startBalance) {

        this.startBalance = startBalance;

    }




	

	

	public int deductBalance(int cost){

		if(cost <= startBalance){

			startBalance -= cost;

		}

		else {

			System.out.println("Insufficient Funds.");

		}

		return startBalance;

			

	}

	

	public int buyPoints(int amt){

		return startBalance += amt;

		

	}

	

	int getBalance()  

	{  

	    return startBalance;  

	}  





}


#23 Hydrogen

Hydrogen
  • Neocodex Co-Founder

  • 22213 posts


Users Awards

Posted 24 November 2011 - 09:19 AM

Very nice! It's an awesome feeling when you finish something and look back at it and can say "yeah... I wrote that :D"

Congratulations :).

#24 Sida

Sida
  • Tsvetesman

  • 3865 posts

Posted 24 November 2011 - 09:22 AM

I just updated the post a little to try and explain 'private' etc :p

Congrats on getting it working ^^

#25 Norava

Norava
  • Pro Can Cran

  • 547 posts


Users Awards

Posted 24 November 2011 - 09:50 AM

Huge thanks to all who helped! (Hydrogen, Sida, Melchoire, InkHeart) I've been struggling with a lot of stuff, you guys are really helping clear it up for me.


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users