主题:求助!急!求高手!
include comments that explains the purpose of each class and method that you write, use well-chosen variable names, and indent/format your code properly. Write well structured code that makes use of helper methods where appropriate. There are three different problems:
1.Modify the Rat class that we developed in lecture during the week before fall break by adding the following methods:
// Returns the difference between this and r.
// In other words, r1.sub(r2) should return r1-r2.
Rat sub (Rat r)
// Returns the quotient of this and r. In other
// words, r1.div(r2) should return r1/r2.
Rat div (Rat r)
// Returns the absolute value of this. In other
// words, r.abs() should return |r|.
Rat abs ()
// Converts this into an approximately equal double.
double toDouble ()
// Returns a negative int if this < r, returns
// a positive int if this > r, and returns zero
// otherwise.
int compareTo (Rat r)
Provide a main method that tests your new methods.
2.In this problem you will write methods that make use of your Rat class from the previous problem. Begin by creating a class called Bisection that contains three static methods: f, bisection, and main.
The method f should take a Rat x as its parameter and should return the Rat x3 - 5x2 + 2x - 3.
The method bisection should take Rats pos, neg, and epsilon as parameters and should return a Rat. If f(pos) <= 0 or f(neg) >= 0 or epsilon <= 0, the method should throw an IllegalArgumentException. Otherwise, it should use the bisection method to find and return a Rat x such that |f(x)| < epsilon. The bisection method is a simple algorithm that can be used to find the root of a continuous function.
The bisection method begins by computing the average of pos and neg; let's call that avg. If |f(avg)| < epsilon, we've found an approximate root and can return it.
If f(avg) > 0, we repeat the previous paragraph using avg as the new value of pos. If f(avg) < 0, we repeat the previous paragraph using avg as the new value of neg. Either way, we've cut the difference between pos and neg in half, and we'll eventually converge to a root.
The method main should contain test cases for bisection. Be sure to display both the root r that is returned as well as the value of f(r). Display the two numbers in both rational and floating point form.
3.Create a class called Date that represents dates consisting of a year, month, and day. A Date object should have the following constructor and methods:
// Creates a new Date object with the given year (which must be positive),
// month (which must be 1 through 12), and day (which must be valid for the
// given month and year). If there are any invalid parameters, the
// constructor should throw an IllegalArgumentException.
public Date (int year, int month, int day)
// Modifies this Date by moving it forward (or backward) the given
// number of days.
public void addDays (int days)
// Returns the number of days this Date must be moved forward (or backward)
// if it is to be equal to d. (Does not, however, modify this Date.)
public int daysTo (Date d)
// Returns the day of this Date.
public int getDay ()
// Returns the month of this Date.
public int getMonth ()
// Returns the year of this Date.
public int getYear ()
// Converts this Date into string form, using the format
// mm/dd/yyyy, as in 07/22/2006.
public String toString ()
// Reports whether or not this and d represent the same date.
public boolean equals (Date d)
// Returns negative if this is earlier than d, returns positive
// if this is later than d, and returns zero otherwise.
public int compareTo (Date d)
Do not use any of the built-in Java date or calendar classes. However, feel free to reuse the date code that you wrote for a previous assignment.
Write a main method that runs test cases on your Date class.
完全没有头绪!!求高手指点!!谢谢。很急。今儿就要交作业了!!!!
1.Modify the Rat class that we developed in lecture during the week before fall break by adding the following methods:
// Returns the difference between this and r.
// In other words, r1.sub(r2) should return r1-r2.
Rat sub (Rat r)
// Returns the quotient of this and r. In other
// words, r1.div(r2) should return r1/r2.
Rat div (Rat r)
// Returns the absolute value of this. In other
// words, r.abs() should return |r|.
Rat abs ()
// Converts this into an approximately equal double.
double toDouble ()
// Returns a negative int if this < r, returns
// a positive int if this > r, and returns zero
// otherwise.
int compareTo (Rat r)
Provide a main method that tests your new methods.
2.In this problem you will write methods that make use of your Rat class from the previous problem. Begin by creating a class called Bisection that contains three static methods: f, bisection, and main.
The method f should take a Rat x as its parameter and should return the Rat x3 - 5x2 + 2x - 3.
The method bisection should take Rats pos, neg, and epsilon as parameters and should return a Rat. If f(pos) <= 0 or f(neg) >= 0 or epsilon <= 0, the method should throw an IllegalArgumentException. Otherwise, it should use the bisection method to find and return a Rat x such that |f(x)| < epsilon. The bisection method is a simple algorithm that can be used to find the root of a continuous function.
The bisection method begins by computing the average of pos and neg; let's call that avg. If |f(avg)| < epsilon, we've found an approximate root and can return it.
If f(avg) > 0, we repeat the previous paragraph using avg as the new value of pos. If f(avg) < 0, we repeat the previous paragraph using avg as the new value of neg. Either way, we've cut the difference between pos and neg in half, and we'll eventually converge to a root.
The method main should contain test cases for bisection. Be sure to display both the root r that is returned as well as the value of f(r). Display the two numbers in both rational and floating point form.
3.Create a class called Date that represents dates consisting of a year, month, and day. A Date object should have the following constructor and methods:
// Creates a new Date object with the given year (which must be positive),
// month (which must be 1 through 12), and day (which must be valid for the
// given month and year). If there are any invalid parameters, the
// constructor should throw an IllegalArgumentException.
public Date (int year, int month, int day)
// Modifies this Date by moving it forward (or backward) the given
// number of days.
public void addDays (int days)
// Returns the number of days this Date must be moved forward (or backward)
// if it is to be equal to d. (Does not, however, modify this Date.)
public int daysTo (Date d)
// Returns the day of this Date.
public int getDay ()
// Returns the month of this Date.
public int getMonth ()
// Returns the year of this Date.
public int getYear ()
// Converts this Date into string form, using the format
// mm/dd/yyyy, as in 07/22/2006.
public String toString ()
// Reports whether or not this and d represent the same date.
public boolean equals (Date d)
// Returns negative if this is earlier than d, returns positive
// if this is later than d, and returns zero otherwise.
public int compareTo (Date d)
Do not use any of the built-in Java date or calendar classes. However, feel free to reuse the date code that you wrote for a previous assignment.
Write a main method that runs test cases on your Date class.
完全没有头绪!!求高手指点!!谢谢。很急。今儿就要交作业了!!!!