r/javahelp • u/ms1244 • Dec 12 '22
I do not understand what hard-coding is and someone told me that my code is hard-coded, could someone help explain and tell me what I have to revise? Thanks.
public class lab6 {
public static void main(String\[\] args) {
final int i = 1;
final int j = 4;
for (int x = 1; x <= j; x++) //A
{
System.out.printf("%1.0f", Math.pow(x, i));
}
System.out.println(" ");
for (int y = 1; y <= j; y++) //A
{
System.out.printf("\*");
}
System.out.println(" ");
for (int z = 1; z <= j; z++) //A
{
System.out.printf("%1.0f*", Math.pow(z, i));
}
System.out.println(" ");
for (int q = 1; q <= j; q++) //B
{
System.out.printf("\*");
System.out.printf("\\n");
}
System.out.println(" ");
for (int r = 1; r <= 4; r++) //C
{
for (int t = 1; t <= 3; t++)
{
System.out.printf("*");
}
System.out.println();
}
System.out.println(" ");
for (int drt = 1; drt <= 9; drt++) //D
{
for (int ikl = 1; ikl <= 15; ikl++)
{
if(drt <= 3 && (ikl <= 6))
{
System.out.printf(" ");
}
else if(drt >= 7 &&(ikl >=10))
{
System.out.printf(" ");
}
else if(drt >= 1 &&(ikl >= 15))
{
System.out.printf(" ");
}
else if(drt >= 7 &&(ikl <= 6))
{
System.out.printf(" ");
}
else if(drt <= 3 &&(ikl >=10))
{
System.out.printf(" ");
}
else if(drt <= 10 &&(ikl <=1))
{
System.out.printf(" ");
}
else
{
System.out.printf("*");
}
}
System.out.println();
}
System.out.println(" ");
for (int eap = 1; eap <= 4; eap++) //Ea
{
for (int klp = 1; klp <= eap; klp++)
{
if(eap == klp)
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
System.out.println(" ");
for (int eab = 4; eab >= 1; eab--) //Eb
{
for (int kld = 1; kld <= eab; kld++)
{
if(eab == kld)
{
System.out.printf("*");
}
else
{
System.out.printf(" ");
}
}
System.out.println();
}
System.out.println(" ");
for (int e = 1; e <= 4; e++) //Fa
{
for (int w = 1; w <= e; w++)
{
System.out.printf("*");
}
System.out.println();
}
System.out.println(" ");
for (int er = 1; er <= 4; er++) //Fb
{
for (int wf = 1; wf < 6 - er; wf++)
{
System.out.printf("*");
}
System.out.println();
}
System.out.println(" ");
for (int as = 1; as <= 4; as++) //Fc
{
for (int gy = 3; gy>= as; gy--)
{
System.out.print(" ");
}
for (int fc = 1; fc <= as; fc++)
{
System.out.printf("*");
}
System.out.println();
}
System.out.println(" ");
for (int df = 0; df <= 4; df++) //Ga
{
for (int gh = 0; gh <= 4 - df; gh++)
{
System.out.printf(" ");
}
for (int k = 1; k <= 2\*df-1 ; k++)
{
System.out.printf("*");
}
System.out.println();
}
System.out.println(" ");
for (int labd = 4; labd > 0; labd--) //Gb
{
for (int qwerty = 0; qwerty <= 4 - labd; qwerty++)
{
System.out.printf(" ");
}
for (int o = 1; o <= 2\*labd-1 ; o++)
{
System.out.printf("*");
}
System.out.println();
}
for (int fd = 0; fd <= 4; fd++) //Gc
{
for (int gf = 0; gf <= 4 - fd; gf++)
{
System.out.printf(" ");
}
for (int f = 1; f <= 2\*fd-1 ; f++)
{
System.out.printf("*");
}
System.out.println();
}
for (int don = 4; don > 0; don--)
{
for (int gof = 0; gof <= 4 - don; gof++)
{
System.out.printf(" ");
}
for (int ktr = 1; ktr <= 2\*don-1 ; ktr++)
{
System.out.printf("*");
}
System.out.println();
}
System.out.println(" ");
System.out.println(" ");
for (int iop = 1; iop <= 21; iop++) //H
{
for (int jhk = 1; jhk <= 30; jhk++)
{
if(iop == 10 && (jhk == 15))
{
System.out.printf(" ");
}
else if(iop == 11 && (jhk == 15))
{
System.out.printf(" ");
}
else if(iop == 9 && (jhk == 15))
{
System.out.printf(" ");
}
else if(iop == 8 && (jhk == 15))
{
System.out.printf(" ");
}
else if(iop == 12 && (jhk == 15))
{
System.out.printf(" ");
}
else if(iop == 11 && (jhk == 16))
{
System.out.printf(" ");
}
else if(iop == 9 && (jhk == 16))
{
System.out.printf(" ");
}
else if(iop == 8 && (jhk == 16))
{
System.out.printf(" ");
}
else if(iop == 12 && (jhk == 16))
{
System.out.printf(" ");
}
else if(iop == 10 && (jhk == 16))
{
System.out.printf(" ");
}
else if(iop == 8 && (jhk == 17))
{
System.out.printf(" ");
}
else if(iop == 9 && (jhk == 17))
{
System.out.printf(" ");
}
else if(iop == 10 && (jhk == 17))
{
System.out.printf(" ");
}
else if(iop == 11 && (jhk == 17))
{
System.out.printf(" ");
}
else if(iop == 12 && (jhk == 17))
{
System.out.printf(" ");
}
else
{
System.out.printf("*");
}
}
System.out.println();
}
}
}31
u/Think_Ad4850 Dec 12 '22
Sorry in advance, I'm scrolling on phone and didn't figure out exactly what output you are creating.
Hard coding is when you as the programmer pre-calculate or assume (especially numbers) and give instructions that are TOO specific.
It's called hard coding because it puts invisible concrete around your program. Examples include running loops a fixed number of times instead of calculating how many times, enumerating every if/else condition when there could be some simpler logic.
It's all about making changes later. You would have to find-and-replace and re-adjust countless things in a hard coded program. You can often change one constant and get a totally different result in an easy-to-change program.
1
14
u/ignotos Dec 12 '22
"Hard coding" means directly including certain special numbers, strings, or other values in your code, even though you might want to change them later on, or to make them configurable somehow.
For example in your code, there are lots of "hard-coded" numbers scattered throughout.
There are two main steps to reducing the amount of hard-coding:
Instead of using numbers directly throughout your code, define constants (e.g. with "static final int GRID_SIZE=4"), and then refer to those elsewhere in your code. This helps to (a) describe what these numbers mean, by giving them names and (b) provides a single convenient place where you can modify them
Make the code "data driven" by not including those numbers in your code at all, but by asking the user for them, loading them from a file, or some other way of configuring your program "from the outside"
9
u/KentuckyWombat Dec 12 '22
You have a lot of numeric literals, especially in conditional statements. Generally you should use a meaningfully named constant for cases like that, so readers of the code know what the intention is. The same applies to string literals.
For example, if (iop == 8 && (jhk == 17)) is one instance of hard coding.
What is the significance of 8 and 17? What does it mean if those variables are equal to those numbers? Using named constants in place of numeric literals (and variables with meaningful names instead of iop and jhk) would make this code much easier to read and understand.
6
u/ratherbealurker Dec 12 '22
You're handling every single part of your program separately. That last for loop looks like it creates a big blocky 'O'? Or a box with a space inside?
******************************
******************************
******************************
******************************
******************************
******************************
******************************
************** *************
************** *************
************** *************
************** *************
************** *************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************
******************************So why handle each little asterisk? You can simple write entire lines of them.
Even just looping 21 times and if the counter is not 8 to 12 write an entire line out of asterisks.
If it is 8-12 then write the other line with a space.
There can be even better solutions and that one is even a little hard coded but no where near what you have now.
simplify your code, boil things down to the least you have to do.
You can reduce all of that code to a few lines and it would be clear what it is doing.
2
u/Grozeth Dec 22 '22
For example if you want to write non-hardcoded version of this, try creating it in a way where you can generate different looking blocks where you can give parameters of how wide, how high, and how big of a hole you want, perhaps even change the character used to fill the block or the "empty" space.
3
u/khooke Extreme Brewer Dec 13 '22
Unrelated question: what is the purpose or all your k, l, gof, variables? You should get in the habit of naming your variables so they convey meaning to another developer. Unless 'gof' has some specific business meaning that your other developers would read and understand, as it is your code is unreadable (meaning, as another developer, I have no idea what your code does).
As a sanity check, always ask yourself "if another developer reads this code, is it clear what this code does?". If the answer is no then you still have work to do.
2
u/MattiDragon Dec 12 '22
Hardcoding, in this case, is when you write your code to do exactly what it's supposed to do instead of creating generic reusable parts that can be used and tweaked easily if requirements change. An other thing that you should look at is your use of System.out.println with a single space in a couple of places, you can just use the argument less version
2
u/Outside-Ad2721 Dec 13 '22 edited Dec 13 '22
I thought I would see if reformatting your code would help at all.
I also thought pasting it to an external post might be practical, because it's so long: https://pastebin.com/96xnwfW1
Looking at your code, the output, the line comments you've added, and the name of your Java class, it looks like this is an assignment with multiple parts.
You have also coded each of the parts, e.g. A, B, C, etc., in the same single method, public static void main(String[] args) .
If I were your instructor and I told you that this was hard-coded, then I might be implying that you should separate each of your answers into separate methods, with good names, and then call those method from within the main method.
Does that make any sense?
Edit: I myself would not really call this hard-coding, but I could see some professors calling it that. With methods you could also take arguments to change your output dynamically by passing in different values for your loops as has been described by others.
1
u/IllegalMigrant Jan 03 '23 edited Jan 04 '23
Hardcoding is using constants (or constant values) instead of variables that are given a value at runtime.
int class_size = 10;
versus
int class_size = getClassSize();
1
u/TheWavefunction Dec 13 '22
Hard coded is when you go against the good practice of putting "important" values in variables, so they are harder to change by the programmer, because you have to change them all throughout the code manually. Also, these hard-coded values cannot be modified at "runtime" so the program should be overall less dynamic.
•
u/AutoModerator Dec 12 '22
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.