[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 24 KB, 640x383, 1288138914845.jpg [View same] [iqdb] [saucenao] [google]
2062619 No.2062619 [Reply] [Original]

Hey /sci/ducks, I have an assignment in Java where I have to take a program I made that computes the monthly interest of a car payment, found here: http://pastebay.com/109836, and turn that into three separate user-defined classes. I tried to work on this yesterday, but it turned into a mess, so I'm wondering how I should go about this.

My professor is awful, so I spent most of my time going off of what friends have told me along with trial and error to get things right, but I can't seem to get it with instantiable classes.

I was thinking of breaking it up into a main class, input class, and output class, but I can't seem to get it to work and make sense. You guys have any ideas?

>> No.2062637

>>2062619

That tattoo is the gayest thing I've seen all day and I just watched line trap get ass fucked.

>> No.2062659

>>2062637

I mean just the sentiment is pretty gay, but then he goes and gets it done in cursive.

>> No.2062663

>>2062659
and misspells "too"

>> No.2062670

>>2062663
twice

>> No.2062714

bumpan

>> No.2062725

>>>/g/

also, that tattoo is gayer than mine, and mines a big fucking dick pointed at my ass

>> No.2062807

>>2062725
Trying there right now, but they're too busy trolling nVidia and Apple fanboys.

Also, shameless bump.

>> No.2062831

No, no that's not how classes work.

You've got to think of what objects there are in your application, what Platonic forms, if you will. Then code their behaviour.

Also, you may want to try and give me incentive to read that code and solve this for you or come up with useful tips.

>> No.2062853

>>2062831
I kind of figured that's not how it worked, and that I probably should get a class that consists entirely of:
"getvariable1"
"setvariable1"
...
I'm just not sure what to do for the third class, since the second class will consist of the main method.

>> No.2062876

>>2062853
Okay, I just saw your code. You're doing this with a fancy GUI, so maybe you could inherit the swing classes you're using there?

So it'd be Main, Logic, GUI

>> No.2062892

>>2062876
So you're suggesting that I do the main method, variable setting, then all of the JOptionPane stuff as the classes?

I'm awful at this so I just need to make sure lol.

>> No.2062918

>>2062663
>>2062670
You're all dumbasses. He clearly meant to write: To be young is to die. To be fast is to live. Bunch of idiots in here.

>> No.2062955

>>2062892
Yeah, something like

public class PaymentsGUI extends JOptionPane {
void info(String info) { JOptionPane.JOptionPane.showInputDialog(null, info);}
}


Or alternatively, you can write your own Throwables. That oughta annoy the teacher.

NegativePriceException
InvalidDownPaymentException
UnnacepatableInterestRateException
UnsatisfactoryDownPaymentToPriceDifference

>> No.2062975

>>2062955
Nice, thanks.

>> No.2063017

>>2062975
Is that it? Give me something, bro.

>> No.2063098

>>2063017
lol, alright then.
I just finished the getvariable and setvariable class, which looks something like
public String setPrice (double P) {
if (P <= 0) {
return "The price of the car must be a positive number greater than zero.";
}
}

public double getPrice () {
return +P;
}

It compiled asking for a return statement after "return "The price of the car must be a positive number greater than zero.";" which I can do, but my question is, is the code right, and should I put that wall of text into the GUI class, or just leave it in the logic class.

>> No.2063156

>>2063098
The compiler demands a certain return statement to exist inside the method's definition.

You return statement is inside an if clause, making it skippable.

You could do something like

public String setPrice (double P) {
if (P <= 0) {
return "The price of the car must be a positive number greater than zero.";
}
return "";
}

But since you've embedded strings in your logic functions, you may need to add exceptions.

Have you been taught exceptions?

>> No.2063207

>>2063156
That's pretty much what I ended up doing.

We haven't been taught much, while, if, and for loops; math functions, and substrings (sort of).

>> No.2063246

>>2063207
Constructors? Exceptions?

>> No.2063261

>>2063246
This project involve constructors, but we spent about 5 minutes on that lol.

>> No.2063285

>>2063261
I have successfully completed your assignment.

I shall delete one line every 5 minutes starting from the last until you give me something in return.

>> No.2063297

>>2063285
Troll?
I'm not quite sure what I have to offer, only thing I can think of is the info to /g/'s FTP server, which is down at the moment.

>> No.2063305 [DELETED] 

>>2063297
I have a list of 20 of them.

>> No.2063313

Oh, btw are you allowed to use java.util.Scanner for input?

If not, I'll have to rewrite this a bit.

>> No.2063329

>>2063313
Probably not, since that sounds fairly complicated lol.
I'm still not sure what I have to offer anyway, I just have a few roms and emulators which are easy to find, and some pirated software, which is also easy to find.

>> No.2063337 [DELETED] 

>>2063313
Actually Scanner simplifies input, but whatever let me rewrite.

The time has been paused.

>> No.2063368

The timer has been restarted.

>> No.2063371

>>2063368
Sorry, I really don't have anything to offer lol.
I have class in about five hours, so I'm going to sleep. Sorry for wasting your time.

>> No.2063377 [DELETED] 

>>2063371
But you haven't. I enjoyed not helping you. Plus I gave you a valuable lesson: Always know what it is others want from you, and what you want from them.

>> No.2063408 [DELETED] 

import javax.swing.*;
import java.text.*;

class Logic {
public Logic(double P, double D, double r, int m) {
if (setPD(P,D) && setR(r) && setM(m)) GUI.info("Your monthly payment is " + ((P-D)*((r/100)/12))/(1-(Math.pow(1+((r/100)/12),-m))));
}
private boolean setPD(double P, double D) {
if (P <= 0) {
GUI.info("The price of the car must be a positive number greater than zero.");
return false;
}
if (D <= 0) {
GUI.info("The down payment of the car must be a positive number greater than zero.");
return false;
}
if (D > P) {
GUI.info("The down payment cannot be more than the price of the car.");
return false;
}
return true;
}
private boolean setR(double r) {
if (r < .08 || r > .5) {
GUI.info("The interest rate must be between .08% and .5%.");
return false;
}
return true;
}

private boolean setM(int m) {
if (m <= 0) {
GUI.info("Cannot have zero or less than zero payments being made on the car.");
return false;
}
return true;
}
}

class GUI {
public GUI() {
new Logic(
Double.parseDouble(JOptionPane.showInputDialog(null, "What is the price of the car?")),
Double.parseDouble(JOptionPane.showInputDialog(null, "How much was the down payment on the car?")),
Double.parseDouble(JOptionPane.showInputDialog(null, "What is the annual interest rate on the car as a percent?")),
Integer.parseInt(JOptionPane.showInputDialog(null, "How many payments are going to be made on the car?"))
);
}
public static void info(String inf) {
JOptionPane.showMessageDialog(null, inf);
}
}

public class ComputePMT {
public static void main(String[] $) {
System.out.println("Computes monthly payment for a car loan given price of a car, down payment, interest rate and number of payments.");
new GUI();
System.exit(0);
}
}

>> No.2063422 [DELETED] 

>>2063408 here

Just for posterity, in case anyone thought I was just trolling. I couldn't resist posting it.

>> No.2063459
File: 37 KB, 1280x1024, assignment.png [View same] [iqdb] [saucenao] [google]
2063459

Here is your assignment.

>> No.2063463 [DELETED] 

Trolololo.

>> No.2063468
File: 144 KB, 1280x1024, BOOM.png [View same] [iqdb] [saucenao] [google]
2063468

But it exploded. :(

>> No.2063478

>>2063459
>>2063468
Oh come on.

>> No.2063481
File: 129 KB, 475x409, trolling expert.jpg [View same] [iqdb] [saucenao] [google]
2063481

>>2063468
picture work out? Any requests?

>> No.2063485

>>2062619
why don't you output those values into minecraft.

>> No.2063502
File: 114 KB, 1280x1024, Clipboard01.png [View same] [iqdb] [saucenao] [google]
2063502

>>2063478

>> No.2063967

OP is probably gone, but I feel the code needs to be posted for posterity. It compiles and works as intended, but OP cannot have. That's what you get for posting homework threads, and asking something for nothing. It's like double hubris all the way.

>> No.2063969

Not yours, cannot have:

import javax.swing.*;
import java.text.*;

class Logic {
public Logic(double P, double D, double r, int m) {
if (setPD(P,D) && setR(r) && setM(m)) GUI.info("Your monthly payment is " + ((P-D)*((r/100)/12))/(1-(Math.pow(1+((r/100)/12),-m))));
}
private boolean setPD(double P, double D) {
if (P <= 0) {
GUI.info("The price of the car must be a positive number greater than zero.");
return false;
}
if (D <= 0) {
GUI.info("The down payment of the car must be a positive number greater than zero.");
return false;
}
if (D > P) {
GUI.info("The down payment cannot be more than the price of the car.");
return false;
}
return true;
}
private boolean setR(double r) {
if (r < .08 || r > .5) {
GUI.info("The interest rate must be between .08% and .5%.");
return false;
}
return true;
}

private boolean setM(int m) {
if (m <= 0) {
GUI.info("Cannot have zero or less than zero payments being made on the car.");
return false;
}
return true;
}
}

class GUI {
public GUI() {
new Logic(
Double.parseDouble(JOptionPane.showInputDialog(null, "What is the price of the car?")),
Double.parseDouble(JOptionPane.showInputDialog(null, "How much was the down payment on the car?")),
Double.parseDouble(JOptionPane.showInputDialog(null, "What is the annual interest rate on the car as a percent?")),
Integer.parseInt(JOptionPane.showInputDialog(null, "How many payments are going to be made on the car?"))
);
}
public static void info(String inf) {
JOptionPane.showMessageDialog(null, inf);
}
}

public class ComputePMT {
public static void main(String[] $) {
System.out.println("Computes monthly payment for a car loan given price of a car, down payment, interest rate and number of payments.");
new GUI();
System.exit(0);
}
}

>> No.2063997

>>2062918
Or perhaps repetition is used as a literary device. And somehow 'young' is a verb, and he's deeply religious.

>> No.2064560

>>2062637
Isn't he a girl now?