Computer Science > STUDY GUIDE > MathTrick.java Florida Virtual School COMPUTER SCIENCE 0200320-61 (All)
/** * * @author Aaron Williams * @version 12/05/21 * * Program uses methods to manipulate integers and strings. * */ public class MathTrick { /** * ****** DO NOT USE ARRAYS OR CON... VERT NUMBERS TO STRING UNTIL STEP 7 ****** * */ // Step 1) Creates a random 3 digit (100-999) number where the first and last digit differ by more than one // Hint: Use Math.random() to create number in the correct range // use modulus to find the last digit and divide by 100 for the first digit. // calculate the difference, which method can you use so it doesn't matter how the numbers are subtract? // while the difference of the digits is less than 2, create a new random number and try again public static int getRandomNum() { int maxNum = 999; int minNum = 100; int num = 0; int firstDigit = 0; int lastDigit = 0; // complete the method while(Math.abs(firstDigit - lastDigit) < 2) { num = (int)(Math.random() * (maxNum - minNum + 1) + minNum); firstDigit = num / 100; lastDigit = num % 10; } return num; } // Step 2 & 4) reverse the digits of a number // Hint: to reverse a number without converting it to a String: // create a new variable to store the reversed number // while the original number > 0 // for each digit, multiply the new variable by 10 (to make room for the new digit) // then add the last digit (remember modulus?) to the new variable // divide the original number by 10 to get rid of the last digit // Ex: 584--> (0 x 10) + 4 = 4 ; 58--> (4 x 10) + 8 = 48; 5-->(48 x 10) + 5 = 485 public static int reverseDigits (int num) { int num2 = 0; while(num > [Show More]
Last updated: 1 year ago
Preview 1 out of 3 pages
Buy this document to get the full access instantly
Instant Download Access after purchase
Add to cartInstant download
We Accept:
Connected school, study & course
About the document
Uploaded On
Nov 20, 2022
Number of pages
3
Written in
This document has been written for:
Uploaded
Nov 20, 2022
Downloads
0
Views
70
In Browsegrades, a student can earn by offering help to other student. Students can help other students with materials by upploading their notes and earn money.
We're available through e-mail, Twitter, Facebook, and live chat.
FAQ
Questions? Leave a message!
Copyright © Browsegrades · High quality services·