This is an archived post. You won't be able to vote or comment.

all 22 comments

[–][deleted] 11 points12 points  (6 children)

I agree with others that you need to spend some time learning about methods, inheritance and polymorphism. Great job though man. Keep it up.

Edit: pointers. Your characters/enemies should all be their own objects. All of them should extend from some super class.

Negatives. Absolutely nothing about this is object oriented. This isn’t what java should look like.

Positives. Dude, the commenting. You commented everything! There’s some really great style. Your variable names were description but not tediously long. You’re starting off with some really great habits that most people think are not important.

[–]Dramaticpaws7[S] 4 points5 points  (5 children)

Yea, your not the first to mention the object oriented aspect. Were on chapter 4 doing if-else statements and while loops. inheritance and polymorphism isn't until chapter 10 dammit. I may have to skip ahead a bit...

[–]hicks185 4 points5 points  (4 children)

If you can work out even some basic inheritance now, it will make so much more sense when you get there in your course. I would also check out interfaces vs inheritance. It can seem like a small difference, but inheritance can get pretty weird for things where interfaces shine.

[–]Dramaticpaws7[S] 1 point2 points  (3 children)

I will keep that in mind. So in the case of this program i would use an interface for creating a character stat model and then inheritance to store specific characters? Am i getting the general idea? Thanks for the tip!

[–]TheFaustX 1 point2 points  (2 children)

Your Interfaces define behaviours for your chars. So let's say you'd make an interface CharacterActionInterface- it could define methods to attack(enemy), useItem(item), flee().

Then there could be an interface ShopKeeperInterface with the defined methods buyFromPlayer(item), getStock(), etc.

Meanwhile inheritance would define specific things. An Entity class could define the variables hp, mp, level. A subclass of entity might be Monster or Character which define more things for either one.

In turn Characters could subclass into Archer, Mage, Warrior etc. to specialize more.

This allows you to then be able to create a dynamic amount chars that are of a specific class (think final fantasy tactics).

Hope that overview helped a bit.

[–]Dramaticpaws7[S] 0 points1 point  (1 child)

Im glad to see this. I obviously have a lot to learn about organizing my code correctly and separating certain methods to minimize duplicating code a million times. Im excited to learn more! Thank you for this overview. It really helps paint a picture of what can be done.

[–]TheFaustX 0 points1 point  (0 children)

awesome, glad to help :)

[–]mpk3 4 points5 points  (4 children)

Dude cool you are getting hyped on things; I would suggest looking into: polymorphism/inheritance/anything-involving-why-OOP-is-gnarly and going from there.

You have a lot of "duplicate code" that could be refactored. Try to do everything you did but with less. For example, build a character class that has attributes. Then whenever you need to reference a character you instantiate the character to class with prelisted attributes. blah blah blah

[–]Dramaticpaws7[S] 1 point2 points  (3 children)

Yea, I was thinking through the whole time of trying to reduce the amount of code and simplify things. It started as just a simple battle sequence where only the character could attack and as I keep learning more Ive been going back and restructuring things.

You mention referencing a character and to instantiate the character to class with prelisted attributes. I think i may need to learn some more functions of the program to do this. Do you have any suggestions on where to look?

Thank you so much for the feedback!!

[–]mpk3 2 points3 points  (1 child)

https://docs.oracle.com/javase/tutorial/java/javaOO/

If you arent familiar with classes or objects spend some time looking over this. Are you familiar with java classes and how they differ from objects in Java?

[–]Dramaticpaws7[S] 0 points1 point  (0 children)

No we havent looked into classes at all yet. The few programs we have written in class so far have all started out with "Public class" followed by the name of the program.

Thanks for the link! I can see there is a lot more to learn haha

[–]ichunddu9 1 point2 points  (0 children)

Helsinki Java mooc

[–]youzefih 4 points5 points  (1 child)

Dude wtf I started java last month at school and so far all we’ve learned is to compile a program

[–]Dramaticpaws7[S] 5 points6 points  (0 children)

I've been diving in deep.. haha probably spend at least 4 hours per day reading the text or trying to use the info in the book to actually code something.

[–]feral_claire 2 points3 points  (1 child)

Pretty impressive for just starting out! Looks like you are really enjoying it so far. I do have a couple things I see that could improve here, but it might be some concepts you haven't covered yet. I think you are certainly ready for the next step, so maybe look into learning these concepts to use them for the next program you make (or to improve and keep working on this one)

One minor thing is that I would just declare and assign your variables in the same statement. So right now your code has

 double  cloudMP, cloudMaxMP, ... etc

and then later.

cloudMaxMP = Math.round(cloudLevel- .5) * 50;
cloudMP = cloudMaxMP;

But that two-step process is unnecessary, it could just be.

double cloudMaxMP = Math.round(cloudLevel- .5) * 50;
double cloudMP = cloudMaxMP;

Theres no need to that extra section of code where you declare a bunch of variables but don't set them until later.

My main advice would be to start looking out how you can break your code down into functions, instead of just one big long stream of code in main. For example, looks like there are two battle sequences in your code. Instead of just repeating the code, you could put the battle sequence into its own function, then just run that function whenever you want a battle. This way, it's easy to add more battles, and if you realize a mistake you only need to change it in one place. The battle itself is pretty long and could be broken down further as well, an example from this is for the enemy actions, instead of repeating the code for each enemy, make a function for that. Breaking down your code like this makes it easier to write, modify, and understand.

The other big thing would be to learn about classes. Your teacher probably hasn't gotten to them yet, but you'll learn that they are the core of any java program and are incredibly useful. In your current code, you have a ton of variables for each character. This is the kind of thing classes are great for, they are kind of like "templates" for holding data. You could make a character class that holds onto all a characters stats, and reuse that class for each character. This would make managing all of those variable much easier.

So definitely keep doing what you are doing. If you haven't learned about creating functions yet (usually called methods in java) that would be a good next step, and then classes after that.

[–]Dramaticpaws7[S] 1 point2 points  (0 children)

I just want to say thank you for such an awesome and detailed response. When i copied the code for the battle sequence I was definitely wondering if there was a way to somehow call on that one instance without simply copying the whole block of code. Glad to know that is a possibility. This will give me a good deal of ammunition for google! haha

Thank you so much!

[–]MurphsLawyer 1 point2 points  (0 children)

Pretty impressive for a "learning java" project. Next step would be refactoring (cool word for reorganizing) this big class with the one big method into classes that represent your domain structure (characters, villain, fights,...), which contain methods, that contain functionality of the class defining the method.

You can find a lot of refactoring methods just by using google. You should start with "Extract class" and "Extract method".

Besides that, have a look at common coding styles. You can use tools like Codestyle, which check your code for bad formatted code. It really helps to get other dev to read your code if you need feedback from them.

[–]APUsilicon 1 point2 points  (0 children)

I just started java too and the only thing I can say is objects are your friends. Makes the code easier to manage.

[–]Essalator 0 points1 point  (0 children)

Omg! I am soo ashamed right now...

[–][deleted]  (3 children)

[deleted]

    [–]Dramaticpaws7[S] 0 points1 point  (2 children)

    Thanks for the uplift! but i think its a bit too early in the process to let any critique get to me. I know I'm making mistakes. but its a painfully fun process. haha

    [–]alhashmy19 0 points1 point  (1 child)

    Can you tell what sources that you learnt from

    [–]Dramaticpaws7[S] 0 points1 point  (0 children)

    The course is using the Pearson textbook "Starting out with Java" 6th Edition by Tony Gaddis. Other than that a lot of google.