# Method to display both solutions to a quadratic

• Notes
• UltraLightningCobra8457
• 13

This preview shows page 7 - 10 out of 13 pages.

// method to display both solutions to a quadratic equation private void displaySolutions(double a, double b, double c) { double root = Math.sqrt(b*b-4*a*c); double solution1 = (-b+root)/2; double solution2 = (-b-root)/2; System.out.print("The solutions are: "); System.out.println(solution1 + ", " + solution2); } (5 marks)

Subscribe to view the full document.

Question/Answer Sheet - Page 8 - CompSci 101 S2 C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED c) Complete the method rotateRight() which accepts a String parameter word, and returns the String rotated one character to the right, e.g. "luck" gives the result "kluc" // method to find a word rotated one character to the right, // e.g. "luck" gives result "kluc" private String rotateRight (String word){ int lastPosition = word.length()-1; char lastChar = word.charAt(lastPosition); String wordFront = word.substring(0,lastPosition); return lastChar + wordFront; } (5 marks) d) Complete the method isAllDigits() which accepts a String parameter, word, and returns true if every character in word is a digit (0, 1, 2, 3, 4, 5, 6, 7, 8, or 9), and false otherwise. You may use the boolean method Character.isDigit(char c) that returns true if its parameter c is a digit and false otherwise. // method to see if a word is all digits private boolean isAllDigits(String word) { boolean allSoFar = true; // all characters so // far tested are digits for (int i = 0; i < word.length(); i++) { if ( Character.isDigit(word.charAt(i)) ) { allSoFar = false; } } return allSoFar; } (5 marks)
Question/Answer Sheet - Page 9 - CompSci 101 S2 C SURNAME: ...................................................... FORENAMES: ........................................................... CONTINUED Question 6 (15 marks) a) What is the output when the following start() method is executed? // mystery program public void start() { String s1 = "****"; String s2 = "* *"; System.out.println(s1); for (int i = 0; i<(s1.length()/2; i++) { System.out.println(s2); } System.out.println(s1); for (int i = 0; i<(s1.length()/2; i++) { System.out.println(s2); } System.out.println(s1); } Show the output here: **** * * * * **** * * * * **** (8 marks) b) What is the output when the following start() method is executed? // owls in the summer public void start() { String s = "toohot"; System.out.println(s + " " + munted(s)); } // mystery helper method private String munted(String s) { String result = ""; for (int j=s.length()-1; j>=0; j--) { result = result + s.charAt(j); } return result; } Show the output here: toohot tohoot (7 marks)

Subscribe to view the full document.

You've reached the end of this preview.
• Summer '12
• Computer Science, Subroutine, ........., The Return, Carriage return

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern