Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

13: caughtSpeeding Write a function in Java that implements the following logic:

ID: 3846870 • Letter: 1

Question

13: caughtSpeeding

Write a function in Java that implements the following logic: You are driving a little too fast, and a police officer stops you. Write code to compute the result, encoded as an int value: 0=no ticket, 1=small ticket, or 2=big ticket. If speed is 60 or less, the result is 0. If speed is between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the result is 2. Unless it is your birthday--on that day, your speed can be 5 higher in all cases.

public int caughtSpeeding(int speed, boolean isBirthday)

{

   

}

19: makeBricks

Write a function in Java to implement the following logic: We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops (hint: use the mod operator %).

public boolean makeBricks(int small, int big, int goal)

{

   

}

22: noTeenSum

Write two methods in Java that implements the following logic: Given 3 int values, a, b, and c, return their sum. However, if any of the values is a teen--in the range 13..19 inclusive--then that value counts as 0, except 15 and 16 do not count as teens. Write a separate helper method called fixTeen() that takes in an int value and returns that value fixed for the teen rule. In this way you avoid repeating the teen code 3 times (i.e. "decomposition").

public int noTeenSum(int a, int b, int c)

{

}

public int fixTeen(int n)

{

   

}

25: evenlySpaced

Write a function in Java that implements the following logic: Given three ints, a, b, and c, one of them is small, one is medium and one is large. Return true if the three values are evenly spaced, so the difference between small and medium is the same as the difference between medium and large.

public boolean evenlySpaced(int a, int b, int c)

{

   

}

26: atFirst

Write a function in Java that implements the following logic: Given a string, return a string made of its first 2 chars. If the string length is less than 2, use '@' for the missing chars.

public String atFirst(String str)

{

   

}

Explanation / Answer

/*please find the following userdefined functions */

package com;

public class UserFunctions {

   public static void main(String args[]) {
       UserFunctions uf = new UserFunctions();
       int caughtSpeed = uf.caughtSpeeding(80, true);
       System.out.println(caughtSpeed);
       boolean bricks = uf.makeBricks(3, 2, 10);
       System.out.println(bricks);
       boolean evenly = uf.evenlySpaced(4, 5, 2);
       System.out.println(evenly);
       String name = uf.atFirst("s");
       System.out.println(name);
       int noteen = uf.noTeenSum(14, 16, 17);
       System.out.println(noteen);

   }

   /*
   * Write a function in Java that implements the following logic: You are
   * driving a little too fast, and a police officer stops you. Write code to
   * compute the result, encoded as an int value: 0=no ticket, 1=small ticket,
   * or 2=big ticket. If speed is 60 or less, the result is 0. If speed is
   * between 61 and 80 inclusive, the result is 1. If speed is 81 or more, the
   * result is 2. Unless it is your birthday--on that day, your speed can be 5
   * higher in all cases.
   */

   public int caughtSpeeding(int speed, boolean isBirthday) {
       int resultTicket = 0;
       if (isBirthday) {
           if (speed <= 65)
               resultTicket = 0;
           if (speed > 65 && speed <= 85)
               resultTicket = 1;
           if (speed > 85)
               resultTicket = 2;
       }
       if (!isBirthday) {
           if (speed <= 60)
               resultTicket = 0;
           if (speed > 60 && speed <= 80)
               resultTicket = 1;
           if (speed > 80)
               resultTicket = 2;
       }
       return resultTicket;
   }

   /*
   * Write a function in Java to implement the following logic: We want to
   * make a row of bricks that is goal inches long. We have a number of small
   * bricks (1 inch each) and big bricks (5 inches each). Return true if it is
   * possible to make the goal by choosing from the given bricks. This is a
   * little harder than it looks and can be done without any loops (hint: use
   * the mod operator %).
   */

   public boolean makeBricks(int small, int big, int goal) {
       if (goal > small + big * 5)
           return false;
       else
           return goal % 5 <= small;
   }

   /*
   * Write a function in Java that implements the following logic: Given three
   * ints, a, b, and c, one of them is small, one is medium and one is large.
   * Return true if the three values are evenly spaced, so the difference
   * between small and medium is the same as the difference between medium and
   * large.
   */

   public boolean evenlySpaced(int a, int b, int c) {
       if (a == b && b == c)
           return true;
       if (a == b || a == c || b == c)
           return false;
       return ((Math.abs(a - b) == Math.abs(b - c))
               || (Math.abs(a - c) == Math.abs(a - b)) || (Math.abs(c - a) == Math
               .abs(b - c)));

   }

   /*
   * Write two methods in Java that implements the following logic: Given 3
   * int values, a, b, and c, return their sum. However, if any of the values
   * is a teen--in the range 13..19 inclusive--then that value counts as 0,
   * except 15 and 16 do not count as teens. Write a separate helper method
   * called fixTeen() that takes in an int value and returns that value fixed
   * for the teen rule. In this way you avoid repeating the teen code 3 times
   * (i.e. "decomposition").
   */

   public int fixTeen(int n) {
       if (n < 13 || n > 19 || n == 15 || n == 16)
           return n;
       return 0;
   }

   public int noTeenSum(int a, int b, int c) {
       return (fixTeen(a) + fixTeen(b) + fixTeen(c));
   }

   /*
   * Write a function in Java that implements the following logic: Given a
   * string, return a string made of its first 2 chars. If the string length
   * is less than 2, use '@' for the missing chars.
   */

   public String atFirst(String str) {
       int len = str.length();
       if (len >= 2)
           return str.substring(0, 2);
       else if (len == 1)
           return (str.charAt(0) + "@");
       else
           return "@@";
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at drjack9650@gmail.com
Chat Now And Get Quote