Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

My Message In My Driver Class Comes Up Blank  RSS feed

 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I have to create a code that creates a magic square based on a number the user inputs. A magic square is a square where the numbers in the vertical, horizontal, and diagonal columns add up to the same number. I was able to do this in one file but now I have to make it work with a driver class. The thing is when I try to print the result it comes up blank. I know this is because the part where I assign the value "Print" is inside the main loop. Is there any way I can get the assigned value of Print out of the main loop and into the driver? Here is my Driver Class



and here is the code connected to the Driver

 
Carey Brown
Bartender
Posts: 2994
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Print is only set on line 89. This is over written each time through the loop. And, you only have it for even matrices, not the odd. You probably should be using a StringBuilder to create the Print String. And all your variable and method names should begin with a lower case letter.
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I may be so blunt, your code formatting is horrific.

There seems to be no rhyme nor reason to how you place your braces, how you indent, how you align. Are you not using an IDE? If you are, there should be a straightforward way to apply standard formatting to your code with a few keystrokes or even a few clicks of the mouse. Please do everyone and yourself a favor and format your code properly, or at least whatever code you post here for others to read.

When you don't format your code properly, it not only reflects poorly on you as a programmer, it also makes it very difficult for others to understand what you've written. I for one have no inclination to read the code that you posted, much less try to help you with your problem and those who have done so are to be commended for their effort.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your choice of variable names is also poor. Even if you format that code correctly, it will still be incomprehensible to anybody else. Come back to that code in three months and you will be unable to understand a line of it.
You have some bad design, including excessive use of the keyword static, methods which seem to do several tasks (that is why the magic() method is so long) and doubtless other things which I do not have to time to look for.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John, after all replies you got so far, nothing to worry a lot, but it seems you'll have to start over mate. What the guys mentioned is very important. Every extra line you write without giving a meaning context to it (wrapping in a method with reasonable name) - makes mess bigger and bigger until you reach the point where you're currently.
As you already noticed, you cannot identify where the issues started and how to fix them, honestly, I don't know too. Usually, if code is nicely written, you should be able to read it through and see a story in it (I stole that word from Junilu), in different words, without an excessive amount of comments you should be able to understand what the program is about. For instance:
As you see from your comments, you're swapping some elements, but those comment would be redundant if that code would be in method with an appropriate name, in addition of that, it would turn out, that half of that code is redundant too.
So you'd have finally

Can you see what is all about? In that manner, if you know that something is wrong with swapping elements, you go to the method swapCornerElements() and debugging it what is wrong, you don't need to look up in 100 lines of code which part is responsible for swapping elements. You can even click ctrl+F and look up for phrase swap corner elements and you'd find it right away the place where you need to look into.
Exactly the same should happen with most of your code, you should decompose your code into small methods, where each of them should be responsible for one task. Those methods shouldn't be longer than 10 lines or so (some of the guys would say even less for a start).

Keep this code as a reference and start over, once you find a description of 5 lines of code what they actually do - make a method; give self revealing name.

I'm sure it won't be easy right away, that is fine, absolutely normal, but in a few weeks, maybe months you'll start getting profit out of it.

So, don't try to debug your code where it flaws now, try to rewrite it by decomposing into methods, and you might even notice yourself where the logic flawed
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:you should be able to read it through and see a story in it (I stole that word from Junilu)

And I "borrowed" it from author Kent Beck
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I am trying a new approach. I know my code looks terrible but I need help on just one section. That section is



I have to try to get the entire magic square into one JOptionPane box but this makes it so each number has it's own box. How can I get them all into one box?
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then let me be even more blunt with you. Not formatting your code, knowing that it's terrible, and not doing anything about it is very INCONSIDERATE of other people who would like to help you. That's like going into someone's house with dirty shoes, tracking mud all over the living room, and saying "I know my shoes are dirty but I have no time to think about you, just help me get this mud off." If I were the house owner, I would kick you out
 
John Sing
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Junilu Lacar wrote:Then let me be even more blunt with you. Not formatting your code, knowing that it's terrible, and not doing anything about it is very INCONSIDERATE of other people who would like to help you. That's like going into someone's house with dirty shoes, tracking mud all over the living room, and saying "I know my shoes are dirty but I have no time to think about you, just help me get this mud off." If I were the house owner, I would kick you out


Let me rephrase that. I'm going to change the way my code looks but I just need to fix this one small section before I do.
 
Campbell Ritchie
Marshal
Posts: 55711
163
  • Likes 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:. . . Let me rephrase that. I'm going to change the way my code looks but I just need to fix this one small section before I do.
Let me rephrase that too.

By not changing the way your code looks you are making it more difficult to fix that one small section. We don't suffer from your poor code; you will when the marks come back.
 
Liutauras Vilda
Marshal
Posts: 4640
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Sing wrote:I'm going to change the way my code looks but I just need to fix this one small section before I do.
You don't.

You really need to change the way you think about programming, preferrably right away. If after all you are still tended to ignore priceless advices given by two well experienced guys above - consider course withdrawal option, it might sounds harsh, but this is what it is. There is no point of studying only because of "paper" you might get. That leads nowhere.

Everyone is here not to moralize, but rather to help and to discuss about right ways of doing software engineering. If it is not on your current goals list, then please come back later when you are ready for that.

After all, guys here are very patient, forgiving and friendy, so don't take everything so hard, but rather do some conclusions and all will be
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!