Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. package com.shpp.p2p.cs.dnapriushkin.assignment3;
  2.  
  3. import com.shpp.cs.a.console.TextProgram;
  4.  
  5.  
  6. public class Assignment3Part3 extends TextProgram {
  7.  
  8. /**
  9. * Condition: Write a program that raises numbers to a power
  10. * Result: The user sets different numbers that the program considers
  11. */
  12. public void run() {
  13.  
  14. double base = 2; // Enter the number you want to raise to the power
  15.  
  16. int exponent = 3; //Enter the degree to which you want to raise numbers
  17. raiseToPower(base, exponent);
  18. }
  19.  
  20. //check number exponent on 0 and 1. we make calculations according to the given numbers
  21. public int raiseToPower(double base, int exponent) {
  22. if (exponent == 0) { // check for 0
  23. return 1;
  24. }
  25.  
  26. if (exponent == 1) {
  27. int result = (int) base;
  28. }
  29. int result = 1;
  30. if (exponent > 1) {
  31. for (int i = 0; i < exponent; i++) { // create a cycle and perform calculations if the degree is positive
  32. result = (int) (result * base);
  33. }
  34.  
  35. } else if (exponent < 0) {
  36. throw new IllegalArgumentException("Error: Exponent Must Be >=0");
  37. }
  38.  
  39. println(result + " result ");
  40.  
  41. return result;
  42. }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement