Advertisement
Guest User

Untitled

a guest
Sep 1st, 2014
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.90 KB | None | 0 0
  1. public class Ch02Homework {
  2.    
  3.     //Use the main subroutine to check your other subroutines.
  4.     //The main subroutine is not graded at all. It can remain blank.
  5.     public static void main(String[] args) {
  6.        
  7.     }
  8.        
  9.     /**
  10.      * This function returns the slope of the line through the points
  11.      * (x1, y1) and (x2, y2).
  12.      * Precondition: The line must have a defined slope.
  13.      * @param x1 the x coordinate of the first point
  14.      * @param y1 the y coordinate of the first point
  15.      * @param x2 the x coordinate of the second point
  16.      * @param y2 the y coordinate of the second point
  17.      * @return the slope of the line
  18.      */
  19.     public static double slope(double x1, double y1, double x2, double y2) {
  20.         return ((y1 - y2) / (x1 - x2));
  21.     }
  22.    
  23.     /**
  24.      * This function returns the geometric mean of number1 and number2
  25.      * Precondition: number1 and number2 must both be positive
  26.      * @param number1 any positive number
  27.      * @param number2 any positive number
  28.      * @return the geometric mean of the numbers
  29.      */
  30.     public static double geometricMean(double number1, double number2) {
  31.         return Math.sqrt(number1 * number2);
  32.     }
  33.    
  34.     /**
  35.      * This function returns the larger of the two solutions to the
  36.      * quadratic equation a*x^2 + b*x + c = 0.
  37.      * Precondition: A solution must exist.
  38.      * @param a the coefficient of the squared term
  39.      * @param b the coefficient of the linear term
  40.      * @param c the constant term
  41.      * @return the larger of the two solutions to the equation
  42.      */
  43.     public static double quadraticSolution(double a, double b, double c) {
  44.         if ((-b + Math.sqrt((b * b) - 4 * a * c))/2 * a) > ((-b - Math.sqrt((b * b) - 4 * a * c))/2 * a){
  45.             return ((-b + Math.sqrt((b * b) - 4 * a * c))/2 * a);
  46.         }
  47.        
  48.         else {
  49.             return ((-b + Math.sqrt((b * b) - 4 * a * c))/2 * a);
  50.         }
  51.     }
  52.    
  53.     /**
  54.      * This function returns a String consisting of the first and last letters
  55.      * of the parameter str.
  56.      * Example: firstAndLast("hello") --> "ho"
  57.      * Example: firstAndLast("x") --> "xx"
  58.      * Precondition: the parameter str must be a String of alpha-characters
  59.      * @param str any non-empty String
  60.      * @return the first and last letters of the parameter str
  61.      */
  62.     public static String firstAndLast(String str) {
  63.         if (str.length() == 1){
  64.             return str + str;
  65.         }
  66.        
  67.         else if (str.length() == 2){
  68.             return str;
  69.         }
  70.        
  71.         else{
  72.             return str.substring(0,0) + charAt(str.length()-1, str.length()); ;
  73.         }
  74.     }
  75.    
  76.     /**
  77.      * This function returns a modified version of the parameter str
  78.      * with the first and last letters lowercase and all the
  79.      * remaining letters capitalized.
  80.      * Example: middleCapitalization("HelLo") --> "hELLo"
  81.      * Example: middleCapitalization("X") --> "x"
  82.      * Example: middleCapitalization("oK") --> "ok"
  83.      * Example: middleCapitalization("") --> ""
  84.      * @param str any String
  85.      * @return a modified version of the parameter str
  86.      */
  87.     public static String middleCapitalization(String str) {
  88.         return "Hello";
  89.     }
  90.    
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement