Midterm 2 Practice

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

Info iconThis 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++; } } }
Background image of page 1

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

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

This document was uploaded on 12/06/2009.

Page1 / 3

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

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online