{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Midterm 2 Practice

Midterm 2 Practice - Midterm 2 Practice-1 Write a method...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Midterm 2 Practice ------------------ 1) Write a method that takes two parameters: an integer "num" & another integer "val". The method generates "num" random numbers & displays how many of these numbers are less than the integer "val". Give the method any name you like. public void problem1(int num, int val) { Random rand = new Random(); int count = 1; int x; int less; while(count <= num){ x = rand.nextInt(); if(x < val){ less++ } count++; } System.out.println(less); } 2) Write a method that takes one integer parameter as input: "multiple". The method displays the first 10 multiples of this number. Example: if multiple = 2 then the output should be: 2 4 6 8 10 12 14 16 18 20 public void problem2(int multiple){ int count = 1; while(count <= 10) { System.out.println(count * multiple); count++; } } * 3) Write a program that asks the user to input an integer. The program displays the reverse of the number. Example: if user enters 123, the output should be 321. 4) Write a method that takes 2 parameters: integer named "start" and an integer named "end". The method displays the sqrt of all numbers starting from "start" & going uptil "end". Also, before doing this make sure that start <= end. If not then you must display an error message. public void problem4(int start, int end) { if(start > end) { System.out.println("Error: start > end"); } else{ int count = start; while(count <= end) { System.out.println(Math.sqrt(count)); count++; } } }
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern