[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Critique my code. Be harsh
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 16
Thread images: 1
File: 1440796356105.jpg (60 KB, 614x642) Image search: [Google]
1440796356105.jpg
60 KB, 614x642
Budding programmer. Wanna learn. How can I improve?
This program is an imaginary scenario to see if the user is eligible for a tax rebate.

import javax.swing.JOptionPane;
public class CarMileage
{
public static void main (String[] args)
{
//---Constant and Variable delcaration---//
final double WORKING_DAYS_PER_WEEK = 5.0; //No work on weekends
final double TO_AND_FROM = 2.0; //Takes into account traveling to AND from work.
final double PERCENT = 100.0; //Decimal result is multiplied by this to show actual percentage

double totalMileage;
double distanceToWork;
double weeksWorkedPerYear;
String userInput; //Handles the JOptionPane

//---User input---//
userInput = JOptionPane.showInputDialog("Enter total mileage");
totalMileage = Double.parseDouble(userInput);

userInput = JOptionPane.showInputDialog("Enter the distance you drive to work (in miles)");
distanceToWork = Double.parseDouble(userInput);

userInput = JOptionPane.showInputDialog("Enter the weeks you work per year");
weeksWorkedPerYear = Double.parseDouble(userInput);

//---Working out % of mileage not commuting to and from work---//
double mileageNotWork = (totalMileage - ((WORKING_DAYS_PER_WEEK * weeksWorkedPerYear) * (distanceToWork * TO_AND_FROM)));

double percentNotCommuting = ((mileageNotWork / totalMileage) * PERCENT);

System.out.println (percentNotCommuting + "% of mileage is not to and from work");

//---Checking if eligible for tax rebate---//
if (percentNotCommuting > 50){
System.out.println ("You are not eligible for a Tax Rebate!");
} else if (percentNotCommuting <= 50){
System.out.println ("You are eligible for a Tax Rebate!");
}
}
}
>>
>>50831493
Oh boy I can't wait to re-compile my binaries every time they change the tax code
>>
You dont always work 5 days a week.
making percent = 100.0 is dumb.
>else if on same line as }
>--- are pointless
>no input validation
enjoy getting hacked, shithead
>>
You should put the final vars into an xml file, or a database, buttmunch
>>
needs more interfaces and factories
>>
>>50831566
I like my elses attached to the end bracket, like

if {
Blah
} else { (newline)
Blah
}

Very much dislike it otherwise, you waste lines and it looks unclean to my eyes. I can follow my own code better and see where elses and other code that isn't forced to the first row much better.

Not op
>>
>>50831705
Syntax fuck up on clover, that last ending is vertically in the same place as the beginning bracket of the else.
>>
>>50831616
This

Or make days working part of user input
>>
>>50831720
Last ending bracket*
Jesus Christ I'm out of it today
>>
>>50831493
>Java
DROPPED

Learn python or c, everything else is absolute garbage.
>>
>>50831493
its ok, but you can do better
>>
>>50832017
found the NEET
>>
>>50831493
>all in one giant main() method
>raw IO instead of get/set methods
>not using javadoc style comments
>asking 4chan about something this simple.

Enjoy being a NEET that can't write modular code.
>>
>>50831493
why are you using the C style on Java?
>>
>>50831515
>>50831566
>>50831616
>>50831652
>>50831705
>>50831720
>>50831743
>>50831744
>>50832017
>>50834009
Try all of these and see which one did the trick.

4chan disguises its greatest geniuses behind a wall of rage.
>>
>>50834068
>geniuses
>implying
Thread replies: 16
Thread images: 1

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.