- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
edit: see next post
icehewk fucked around with this message at 05:59 on Feb 26, 2008
|
#
¿
Feb 26, 2008 04:13
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 29, 2024 02:04
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
After six weeks of my first programming course, I know absolutely nothing. The instructor doesn't teach along with the book, nor does he teach much of the ideas behind the code. To borrow a lovely analogy, it's like he goes over what you can nail where without explaining where the spot to be nailed is located or why it's being nailed. As such, I've taken it upon myself through the resources provided here to learn Java outside of class and hope I can catch up before the next project is assigned. Unfortunately, my latest project is due Wednesday and I don't know where to begin. Can you guys help?
quote:CS 172
Project 2 (due February 27/28)
Develop and test a RationalNumber class that allows exact fraction arithmetic (floating-point arithmetic is approximate). We will enhance this class in Project 3 so that, among other things, RationalNumber objects always store fractions reduced to lowest terms. RationalNumber’s should follow the same rules about writing and doing arithmetic with fractions that you learned about in fifth grade. Your RationalNumber class should behave as illustrated by the code in the following test driver and the output that is generated by running that driver. You should develop and test the class one feature at a time (use a short code-test-debug cycle so that you are never more than a few lines of code away from working Java code), using statements like those in the driver below, adding and testing features approximately in the order suggested by the driver:
code:public class Project3Driver {
public static void main(String[] args) {
RationalNumber r1 = new RationalNumber(4, 6);
System.out.println("r1 = " + r1); // equivalent to "r1 + " + r1.toString()
RationalNumber r2 = new RationalNumber(-3, 1);
System.out.println("r2 = " + r2);
RationalNumber r3 = new RationalNumber(0, 5);
System.out.println("r3 = " + r3);
RationalNumber r4 = new RationalNumber(1, -3);
System.out.println("r4 = " + r4);
RationalNumber r5 = new RationalNumber(-1, -3);
System.out.println("r5 = " + r5);
//RationalNumber r6 = new RationalNumber(-2, 0); // generates error
// message and exits the application
RationalNumber r7 = new RationalNumber(-2, -3);
RationalNumber threeFourths = new RationalNumber(3, 4);
RationalNumber fiveSixths = new RationalNumber(5, 6);
System.out.println("3/4 + 5/6 = " + threeFourths.plus(fiveSixths));
System.out.println("3/4 - 5/6 = " + threeFourths.minus(fiveSixths));
System.out.println("3/4 * 5/6 = " + threeFourths.times(fiveSixths));
System.out.println("3/4 / 5/6 = " + threeFourths.dividedBy(fiveSixths));
RationalNumber oneFourth = new RationalNumber(1, 4);
System.out.println(Math.pow(16, -oneFourth.doubleApproximation()));
System.exit(0); // This is superfluous here but necessary if
// if the application uses swing components
}
}
Output:
r1 = 4/6
r2 = -3
r3 = 0
r4 = 1/-3
r5 = -1/-3
3/4 + 5/6 = 38/24
3/4 - 5/6 = -2/24
3/4 * 5/6 = 15/24
3/4 / 5/6 = 18/20
0.5
The following gives a skeleton version of the RationalNumber class with some hints to help you supply the missing code.
code:public class RationalNumber {
// data
private int numerator;
private int denominator;
// constructors
public RationalNumber(){
numerator = 0;
denominator = 1;
}
public RationalNumber(int numeratorIn, int denominatorIn){
if (denominatorIn == 0){
System.out.println("Denominator can't be zero");
System.exit(0);
}
numerator = numeratorIn;
denominator = denominatorIn;
//if (numerator != 0) WE’LL ADD THIS IN PROJECT 3
// reduce();
}
// public methods
public String toString(){
// include code here that handles the cases where numerator = 0 or
// denominator = 1
}
public RationalNumber plus(RationalNumber r){
// just think of the rules for adding fractions
}
public RationalNumber times(RationalNumber r){
// just think of the rules for multiplying fractions
}
public RationalNumber dividedBy(RationalNumber r){
if (r.numerator == 0){
throw new ArithmeticException();
}
else{
// use the fact that division is multiplication by the inverse
}
}
public RationalNumber minus(RationalNumber r){
// just think of the rules for subtracting fractions
}
public double doubleApproximation(){
// just divide numerator by denominator. Make sure it’s floating-point
// division.
}
} // class RationalNumber
|
#
¿
Feb 26, 2008 06:05
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
Incoherence posted:
Let's walk through plus(): First, how do you add fractions? If you have two fractions a/b and c/d, what is their sum in terms of a, b, c, and d? Second, which field of which object corresponds to a, b, c, and d?
It would be a+c when b=d. How would you go about finding the LCD? Is the data field the part that corresponds to a, b, c and d?
|
#
¿
Feb 26, 2008 18:15
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
Incoherence posted:
No, if b = d, then it'd be (a+c)/b. I'm being pedantic here for a reason: this isn't a Java problem, but an algorithmic problem, and if you can't specify how to add fractions algebraically how do you expect to tell the computer how to?
So what would I write to find the common denominator? I really don't understand much of the actual language beyond syntax and reference.
|
#
¿
Feb 26, 2008 21:29
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
Seems the uni knows how tough the class is so they provide a free tutor three times a week. Algebraically, a/b and c/d would require b*d and d*b along with a*d and c*b. That would be (a*d)+(c*b) over (b*d), right?
icehewk fucked around with this message at 00:15 on Feb 27, 2008
|
#
¿
Feb 27, 2008 00:08
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
I'm doing the 'create a payroll program' assignment and having some trouble with the 'double' lines.
Here's what I've got so far:
code:import java.text.*;
import java.util.*;
public class Payroll {
public static void main(String[] args)
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String input = "";
System.out.print("Enter employee's name: ");
input = in.readLine();
String name = new String(input);
System.out.print("Enter hours worked in a week: ");
input = in.readLine();
String hours = new String(input);
System.out.print("Enter hourly pay rate: ");
input = in.readLine();
String payrate = new String(input);
System.out.print("Enter federal tax withholding rate: ");
input = in.readLine();
String fed = new String(input);
System.out.print("Enter state tax rate: ");
input = in.readLine();
String state = new String(input);
DecimalFormat df = new DecimalFormat("0.00");
Scanner scan = new Scanner(System.in);
System.out.print("Hours worked: "+ hours);
System.out.print("Pay Rate: $" + rate);
double gross = ( rate.doubleValue() * hours.doubleValue();
System.out.print("Gross Pay: $" + df.format(gross);
double taxfed = ( fed.doubleValue() * gross.doubleValue();
double taxstate = ( state.doubleValue() * gross.doubleValue();
double taxnet = ( taxfed.doubleValue() + taxstate.doubleValue();
System.out.println("Deductions:"
+ "\n\n"
+ "Federal Withholding <20.0%>: $" + df.format(taxfed) + "\n"
+ "State Withholding <9.0%>: $" + df.format(taxstate) + "\n"
+ "Total Deduction: $" + df.format(taxnet));
System.out.println("Net Pay: $" + ( gross - taxnet ));
}
}
|
#
¿
Sep 18, 2008 00:22
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
I'm having quite a bit of trouble, could you hit me up on AIM? icehewk
|
#
¿
Sep 18, 2008 02:30
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
I'm still getting a 'few' errors. I think I'm missing something from the beginning
code:import java.text.*;
import java.util.*;
public class Payroll {
public static void main(String[] args)
{
System.out.print("Enter employee's name: ");
Input = In.readLine();
name = input;
System.out.print("Enter hours worked in a week: ");
hours = in.readLine();
System.out.print("Enter hourly pay rate: ");
payrate = in.readLine();
System.out.print("Enter federal tax withholding rate: ");
fed = in.readLine();
System.out.print("Enter state tax rate: ");
state = in.readLine();
DecimalFormat df = new DecimalFormat("0.00");
Scanner scan = new Scanner(System.in);
System.out.print("Hours worked: "+ Hours);
System.out.print("Pay Rate: $" + Rate);
double gross = ( Double.parseDouble(rate) * Double.parseDouble(hours) );
System.out.print("Gross Pay: $" + (gross));
double taxfed = ( Double.parseDouble(fed) * Double.parseDouble(gross) );
double taxstate = ( Double.parseDouble(state) * Double.parseDouble(gross) );
double taxnet = ( Double.parseDouble(taxfed) + Double.parseDouble(taxstate) );
System.out.println("Deductions:"
+ "\n\n"
+ "Federal Withholding <20.0%>: $" + df.format(taxfed) + "\n"
+ "State Withholding <9.0%>: $" + df.format(taxstate) + "\n"
+ "Total Deduction: $" + df.format(taxnet));
System.out.println("Net Pay: $" + (gross - taxnet));
}
}
code:C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:10: cannot find symbol
symbol : variable Input
location: class Payroll
Input = In.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:10: cannot find symbol
symbol : variable In
location: class Payroll
Input = In.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:11: cannot find symbol
symbol : variable name
location: class Payroll
name = input;
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:11: cannot find symbol
symbol : variable input
location: class Payroll
name = input;
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:14: cannot find symbol
symbol : variable hours
location: class Payroll
hours = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:14: cannot find symbol
symbol : variable in
location: class Payroll
hours = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:17: cannot find symbol
symbol : variable payrate
location: class Payroll
payrate = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:17: cannot find symbol
symbol : variable in
location: class Payroll
payrate = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:20: cannot find symbol
symbol : variable fed
location: class Payroll
fed = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:20: cannot find symbol
symbol : variable in
location: class Payroll
fed = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:23: cannot find symbol
symbol : variable state
location: class Payroll
state = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:23: cannot find symbol
symbol : variable in
location: class Payroll
state = in.readLine();
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:29: cannot find symbol
symbol : variable Hours
location: class Payroll
System.out.print("Hours worked: "+ Hours);
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:30: cannot find symbol
symbol : variable Rate
location: class Payroll
System.out.print("Pay Rate: $" + Rate);
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:32: cannot find symbol
symbol : variable rate
location: class Payroll
double gross = ( Double.parseDouble(rate) * Double.parseDouble(hours) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:32: cannot find symbol
symbol : variable hours
location: class Payroll
double gross = ( Double.parseDouble(rate) * Double.parseDouble(hours) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:35: cannot find symbol
symbol : variable fed
location: class Payroll
double taxfed = ( Double.parseDouble(fed) * Double.parseDouble(gross) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:35: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (double)
double taxfed = ( Double.parseDouble(fed) * Double.parseDouble(gross) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:36: cannot find symbol
symbol : variable state
location: class Payroll
double taxstate = ( Double.parseDouble(state) * Double.parseDouble(gross) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:36: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (double)
double taxstate = ( Double.parseDouble(state) * Double.parseDouble(gross) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:37: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (double)
double taxnet = ( Double.parseDouble(taxfed) + Double.parseDouble(taxstate) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:37: parseDouble(java.lang.String) in java.lang.Double cannot be applied to (double)
double taxnet = ( Double.parseDouble(taxfed) + Double.parseDouble(taxstate) );
^
C:\Documents and Settings\Chris\Desktop\Homework\Java\Payroll.java:37: incompatible types
found : java.lang.String
required: double
double taxnet = ( Double.parseDouble(taxfed) + Double.parseDouble(taxstate) );
^
23 errors
Tool completed with exit code 1
|
#
¿
Sep 19, 2008 03:28
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
It does offer tutoring, when the tutor remembers to show up. Thanks for your help and patience. I'll definitely get the basics down before I post again.
edit: I got it running perfectly thanks to your suggestions and a little error googling. Thanks!
icehewk fucked around with this message at 07:19 on Sep 19, 2008
|
#
¿
Sep 19, 2008 06:21
|
|
- Adbot
-
ADBOT LOVES YOU
|
|
#
¿
Apr 29, 2024 02:04
|
|
- icehewk
- Jul 7, 2003
-
Congratulations on not getting fit in 2011!
|
Edit: got it.
icehewk fucked around with this message at 00:07 on Oct 5, 2008
|
#
¿
Oct 4, 2008 23:51
|
|