Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ch02Homework {
- //Use the main subroutine to check your other subroutines.
- //The main subroutine is not graded at all. It can remain blank.
- public static void main(String[] args) {
- }
- /**
- * This function returns the slope of the line through the points
- * (x1, y1) and (x2, y2).
- * Precondition: The line must have a defined slope.
- * @param x1 the x coordinate of the first point
- * @param y1 the y coordinate of the first point
- * @param x2 the x coordinate of the second point
- * @param y2 the y coordinate of the second point
- * @return the slope of the line
- */
- public static double slope(double x1, double y1, double x2, double y2) {
- return ((y1 - y2) / (x1 - x2));
- }
- /**
- * This function returns the geometric mean of number1 and number2
- * Precondition: number1 and number2 must both be positive
- * @param number1 any positive number
- * @param number2 any positive number
- * @return the geometric mean of the numbers
- */
- public static double geometricMean(double number1, double number2) {
- return Math.sqrt(number1 * number2);
- }
- /**
- * This function returns the larger of the two solutions to the
- * quadratic equation a*x^2 + b*x + c = 0.
- * Precondition: A solution must exist.
- * @param a the coefficient of the squared term
- * @param b the coefficient of the linear term
- * @param c the constant term
- * @return the larger of the two solutions to the equation
- */
- public static double quadraticSolution(double a, double b, double c) {
- if ((-b + Math.sqrt((b * b) - 4 * a * c))/2 * a) > ((-b - Math.sqrt((b * b) - 4 * a * c))/2 * a){
- return ((-b + Math.sqrt((b * b) - 4 * a * c))/2 * a);
- }
- else {
- return ((-b + Math.sqrt((b * b) - 4 * a * c))/2 * a);
- }
- }
- /**
- * This function returns a String consisting of the first and last letters
- * of the parameter str.
- * Example: firstAndLast("hello") --> "ho"
- * Example: firstAndLast("x") --> "xx"
- * Precondition: the parameter str must be a String of alpha-characters
- * @param str any non-empty String
- * @return the first and last letters of the parameter str
- */
- public static String firstAndLast(String str) {
- if (str.length() == 1){
- return str + str;
- }
- else if (str.length() == 2){
- return str;
- }
- else{
- return str.substring(0,0) + charAt(str.length()-1, str.length()); ;
- }
- }
- /**
- * This function returns a modified version of the parameter str
- * with the first and last letters lowercase and all the
- * remaining letters capitalized.
- * Example: middleCapitalization("HelLo") --> "hELLo"
- * Example: middleCapitalization("X") --> "x"
- * Example: middleCapitalization("oK") --> "ok"
- * Example: middleCapitalization("") --> ""
- * @param str any String
- * @return a modified version of the parameter str
- */
- public static String middleCapitalization(String str) {
- return "Hello";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement