INSTRUCTIONSIn an Eclipse project, set up files hierarchy and names following these instructions:Package name:labs.lab2Create a single java file/class in this package with the name"Main"Use this...



INSTRUCTIONS



  1. In an Eclipse project, set up files hierarchy and names following these instructions:

    1. Package name:labs.lab2

    2. Create a single java file/class in this package with the name"Main"

    3. Use this template: (
      Main.java


      Download Main.java


      )to write your code for problems 2, 3, 4, 5, 6, 7, 8, and 10 (you can also test all your code for both parts here). Notice each method has a specific name, and that each function returns a specific datatype.

    4. Add classes Calculator and Rectangle.

    5. Make sure your methods pass all the examples/test cases suggested.Keep in mind that for grading, we will test other additional values to the ones provided here.You can write code to test your methods in themainmethod of the Main.java file. You can also write test cases in the JUnit test file for this lab, which will be provided in a link below.



  2. For submission:



    1. Upload Calculator.java, Main.java, and Rectangle.java to the Gradescope assignment.Run the sample autograder (which uses the below JUnit test file) as many times as you wish up until the deadline. The test file used for grading will be similar to this one, but will contain different test cases, as well as additional test cases. So be sure to carefully and thoroughly do your own testing of your code as well.

    2. Be sure to read theLab Submission Checklistbefore submitting your code to Gradescope.






JUNIT TEST FILE FOR LAB2:
Lab2Test.java


Download Lab2Test.java






PROBLEM PROMPTS



Part A:




Problem 1: Calculator (10 pts)




Implement a class
Calculator
whose constructor receives two integers and provides methods for getting:




  • The sum of the two numbers


  • The difference between the two numbers (first minus second)


  • The product


  • The average


  • The distance (absolute value of the difference)


  • The maximum (larger of the two)


  • The minimum (smaller of the two)


Use the template provided here:
Calculator.java


Download Calculator.java



Example:


Calculator c1 = new Calculator(5, 10);
c1.getSum(); // returns 15
c1.getDifference(); // returns -5
c1.getProduct(); // returns 50
c1.getAverage(); // returns 7.5
c1.getDistance(); // returns 5
c1.getMax(); // returns 10
c1.getMin(); // returns 5





problem2_printWithCommas (10 pts)



Write a method that reads a number between 1,000 and 999,999 from the user and prints it with a comma separating the thousands.


When you print the number, useprintinstead ofprintln(this will make testing/grading easier, since the newline separator that gets inserted usingprintlnvaries per platform).


You can assume all user inputs will be valid integers between 1,000 and 999,999 (inclusive).


Example:


Please enter an integer between 1000 and 999999: 892302
892,302





problem3_calculateTotal (10 pts)


For this problem, consider a sticker store in which all stickers are the same price. Write a method that computes the total of a sticker order based on the following inputs, which are prompted for from the user:


1. The price per sticker


2. The number of stickers in the order


The price of the order is the sum of:



  • The total price of the stickers

  • The tax (10 percent of the total price of the stickers)

  • The shipping charge ($0.15 per sticker)


Print the price of the order, formatted as money, with a dollar sign and two digits after the decimal point. Assume the user will always input a valid price per sticker (a valid floating point number >= 0), and a valid number of stickers (an integer >= 1).


When you print the number, useprintinstead ofprintln(this will make testing/grading easier, since the newline separator that gets inserted usingprintlnvaries per platform).



Example:


Enter price per sticker: 1.00
Enter the number of stickers: 10
Your total is: $12.50







problem4_compoundInterest(10 pts)


Write a method that computes the value of an investment compounded over time. The program must ask for the starting amount, the number of years to invest, the interest rate, and the number of periods per year to compound. The formula you'll use for this is:





A=P(1+rn)nt" role="presentation" style="display: inline-block; line-height: normal; font-size: 16px; text-align: left; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; position: relative;">A=P(1+rn)nt




where




  • Pis the principal amount (double)


  • ris the annual rate of interest (double)


  • tis the number of years the amount is invested (int)


  • nis the number of times the interest is compounded per year (int)


  • Ais the amount at the end of the investment


Your program must work like the following example. When you print the result, format/round all monetary amounts to two decimal places after the decimal point. Assume all user input will be of valid type and value (>= 0).


When you print the number, useprintinstead ofprintln(this will make testing/grading easier, since the newline separator that gets inserted usingprintlnvaries per platform).



Example:


Enter principal amount: 1500
Enter the annual rate of interest: 4.3
Enter the number of years the amount is invested: 6
Enter the number of times the interest is compounded per year: 4
$1500.00 invested at 4.3% for 6 years compounded 4 times annually is $1938.84.





problem5_creditCardPayoff(10 pts)


Write a method that will help you figure out how many months it will take to pay off a credit card balance. The program must ask the user to enter the balance of a credit card the the APR for the card. The method will return the number of months required to pay off the card (if the result is a fractional amount, round up to the nearest int).


The formula you'll use for this is:





n=130ln(1+bp(1(1+i)30))ln(1+i)" role="presentation" style="display: inline-block; line-height: normal; font-size: 16px; text-align: left; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; padding: 0px; margin: 0px; position: relative;">n=130ln(1+bp(1(1+i)30))ln(1+i)




where




  • nis the number of months


  • iis the daily rate (APR divided by 365)

    • Accept the APR input as a percentage, not a decimal, then do the division internally

    • Assume APR will always be > 0 and <>




  • bis the balance

    • Assume balance will always be >= 0




  • pis the monthly payment

    • Assume monthly payment will always be > 0




Your program must work like the following example.


When you print the number, useprintinstead ofprintln(this will make testing/grading easier, since the newline separator that gets inserted usingprintlnvaries per platform).



Example:


What is your balance? 21394.88
What is the APR on the card? 5.9
What is the monthly payment you can make? 2000
It will take you 12 months to pay off this card.








Part B:




problem6_troubleshootCarIssues (10 pts)



Anexpert systemis a type of artificial intelligence program that uses a knowledge base and a set of rules to perform a task that a human expert might do. Often, these are implemented usingchat bots, which can conduct on-line chat conversations via text or text-to-speech, in lieu of providing direct contact with a live human agent.


Create a simple chat bot expert system that walks the user through troubleshooting issues with a car. Use the following decision tree to build the system:



IMG_5038 2 (1).jpeg



Your program must work like the following example. Accept any input that starts with 'y' or 'Y' as a "yes", and any input that starts with 'n' or 'N' as a "no." Any other input should cause the program to print "Invalid input. Exiting." and end the program.


When you print the number, useprintinstead ofprintln(this will make testing/grading easier, since the newline separator that gets inserted usingprintlnvaries per platform).



Example:


Is the car silent when you turn the key? y
Are the battery terminals corroded? NO SIR
Replace cables and try again.





problem7_assessPasswordStrength (10 pts)



Create a method that determines the strength of a given password based on these rules:



  • A very weak password contains only digits and is fewer than eight characters

  • A weak password contains only letters and is fewer than eight characters

  • A strong password contains at least one letter and at least one digit and is at least eight characters

  • A very strong password contains at least one letter, at least one digit, and at least one special character (non letter or digit) and is at least eight characters

  • All other passwords are medium strength


You can assume all input strings will have a length >= 1.




Examples:


problem7_assessPasswordStrength("12345"); // returns "very weak"
problem7_assessPasswordStrength("abcdef"); // returns "weak"
problem7_assessPasswordStrength("abc123xyz"); // returns "strong"
problem7_assessPasswordStrength("1337h@xor"); // returns "very strong"
problem7_assessPasswordStrength("1337h xor"); // returns "very strong"
problem7_assessPasswordStrength("123abc"); // returns "medium"





problem8_getNumericGrade and problem8_getLetterGrade (10 pts)



Implement two methods:
problem8_getNumericGrade and problem8_getLetterGrade.



problem8_getNumericGradetranslates a letter grade into a number grade. Letter grades are A, B, C, D, and F (case insensitive), possibly followed by + or -. Their numeric values are 4, 3, 2, 1, and 0. There is no F+ or F-. A plus sign increases the numeric value by 0.3, and a minus sign decreases it by 0.3. However, an A+ has value 4.0. If the input is not a valid letter grade, return -1.



problem8_getLetterGradetranslates a numeric grade (a number between 0 and 4, inclusive, that means this range includes the number 0 and the number 4) into the closest letter grade. For example, the number 2.8 (which might have been the average of several grades) would be converted to B-. Break ties in favor of the better grade; for example 2.85 should be a B. If the input is not a valid number between 0 and 4, return the string "Error."


Conversion table from input to output forproblem8_getLetterGrade:

































































InputOutput
>=0 and <>F
>= 0.5 and <>D-
>= 0.85 and <>D
>= 1.15 and <>D+
>= 1.5 and <>C-
>= 1.85 and <>C
>= 2.15 and <>C+
>= 2.5 and <>B-
>= 2.85 and <>B
>= 3.15 and <>B+
>= 3.5 and <>A-
>= 3.85 and <>A
4.0A+
Anything elseError



Examples:


problem8_getNumericGrade("G") // returns -1.0
problem8_getNumericGrade("a-") // returns 3.7
problem8_getNumericGrade("C+") // returns 2.3
problem8_getNumericGrade("B") // returns 3.0
problem8_getLetterGrade(2.85) // returns "B"
problem8_getLetterGrade(0.51) // returns "D-"
problem8_getLetterGrade(4.0) // returns "A+"
problem8_getLetterGrade(8.3) // returns "Error"





Problem 9: Rectangle (10 pts)



Implement a classRectangle. ARectangleis defined by its x- and y-coordinates of its upper-left and lower-right corners.




Your job is to determine whether the rectangle is a square, or is in "portrait" (vertically-oriented) or "landscape" (horizontally-oriented) orientation, and return the result in thegetOrientationmethod. Assume all coordinates will be validdoublevalues. Implement theRectangleclass using the template provided here:
Rectangle.java


Download Rectangle.java





Examples:


Rectangle r1 = new Rectangle(10.5, 22.5, 20.5, 12.5);
r1.getOrientation(); // returns "square"
Rectangle r2 = new Rectangle(10.5, 32.5, 21.5, 12.5);
r2.getOrientation(); // returns "portrait"
Rectangle r3 = new Rectangle(-10.5, 22.5, 21.5, 12.5);
r3.getOrientation(); // returns "landscape"





problem10_withoutX2 (10 pts)



Write a method that accepts aStringof any length as a parameter. If one or both of the first 2 chars in the string is the char 'x' (lower case only), return the string without those 'x' chars. Otherwise, return the string unchanged.




Examples:


problem10_withoutX2("xHi") // returns "Hi"
problem10_withoutX2("Hxi") // returns "Hi"
problem10_withoutX2("Hi") // returns "Hi"
problem10_withoutX2("XHi") // returns "XHi"


Oct 11, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here