For a given input value N, find 3^N (Three to the power of N) . You must use a loop! ^_^ (i.e. Don't use Math.pow() if you want full credit in test) getThreeToThePowerOf(0) → 1 getThreeToThePowerOf(1) → 3 getThreeToThePowerOf(3) → 27
public int getThreeToThePowerOf(int N) { |
import java.util.Scanner;
public class ThreeToThePowerOf {
public static void main(String[]args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the exponent value for 3: ");
String userIn = sc.nextLine().trim();
while(!isDigit(userIn))
{
System.out.print("\nEnter the exponent value for 3: ");
userIn = sc.nextLine().trim();
}
int exponent = Integer.parseInt(userIn);
System.out.println("\nOUTPUT: 3^" + exponent + " = " +
getThreeToThePowerOf(exponent) + "\n");
}
/** This method returns 3 raised to the power of N using
loop
*
* @param N the exponential value
* @return 3 raised to the power of N
*/
public static int getThreeToThePowerOf(int N)
{
int result = 1;
for(int i = 1; i <= N; i++)
{
result *= 3;
}
return result;
}
/** This method returns TRUE if user input is an integer, else
returns FALSE
*
* @param s the user input string
* @return true if the user input gets parsed to integer value, else
returns false
*/
public static boolean isDigit(String s)
{
try
{
Integer.parseInt(s);
return true;
}catch(NumberFormatException nfe){
System.out.println(s + " is not a valid integer!");
return false;
}
}
}
******************************************************************** SCREENSHOT *******************************************************
For a given input value N, find 3^N (Three to the power of N) . You...
static public int[] Question() // retrieve an integer 'n' from the console // the first input WILL be a valid integer, for 'n' only. // then read in a further 'n' integers into an array. // in the case of bad values, ignore them, and continue reading // values until enough integers have been read. // this question should never print "Bad Input" like in lab // hint: make subfunctions to reduce your code complexity return null; static public int...
I am given an input file, P1input.txt and I have to write code to find the min and max, as well as prime and perfect numbers from the input file. P1input.txt contains a hundred integers. Why doesn't my code compile properly to show me all the numbers? It just stops and displays usage: C:\> java Project1 P1input.txt 1 30 import java.io.*; // BufferedReader import java.util.*; // Scanner to read from a text file public class Project1 { public static...
You will write three static methods to manipulate an input String in different ways using various String methods. You need to provide the code for each of the three static methods in class StringPlay (requirements for each listed below). You will also change the control statement in the test harness to allow for variations in the sentinel value. You need to modify the loop control condition in Lab09.java so that user inputs of ‘finish’, “FINISH”, “FiniSH”, “fINISH”, etc. will end...
ALSO: d) What is the power of N(t) in dBm? (use No = 4 10-21W/Hz) AND: e) What is the formula for the autocorrelation function RN(τ) ? (leave the answer with No) 4) Let W(t) be AWGN. a) Sketch the PSD of W(t). (Note: This item is not graded in Blackboard. You must hand in your HW to get points for this item.) You can use No to indicate amplitude in your sketch (i.e., no need to replace with the...
Find the maximum value and minimum value in milesTracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program: Find the maximum value and minimum value in miles Tracker. Assign the maximum value to maxMiles, and the minimum value to minMiles. Sample output for the given program Min miles: -10 Max miles: 40 (Notes) 1 import java.util.Scanner 3 public class ArraysKeyValue 4 public static void main (String passe args) i final int...
CODE MUST BE IN C 1. Given an int variable n that has been initialized to a positive value and, in addition, int variables k and total that have alreadybeen declared, use a while loop to compute the sum of the cubes of the first n whole numbers, and store this value in total. Thus if n equals 4, your code should put 1*1*1 + 2*2*2 + 3*3*3 + 4*4*4 into total. Use no variables other than n, k, and...
IN JAVA please Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. Your code will be tested for runtime. Code which does not output a result in logarithmic time (making roughly log(2) N comparisons) will fail the tests. A sample main function is provided so that you may test your code on sample inputs. For testing purposes, the...
(25 points) Factorize Write a String method factorize(int n, int m) that when given a positive integer n and an integer m, returns a String representation of how many times n factors into m. Must use a while loop to get full credit. (25 points) Print Product Table Write a void method printProductTable(int n, int m) that when given two positive integers n and m, prints an n (rows) by m (columns) product table. Note that entries in the table...
When you want a loop to iterate exactly n times, you will typically use one of two standard for loops patterns (see p. 95-96): for (int i = 1; i <= n; i++) for (int i = 0; i < n; i++) Although both of these for loops will output n times, they have different initialization and test conditions. Please give two *examples to describe why it is beneficial to have both patterns. (i.e. think of the initialization condition and...
Problem Description proving program correctness Consider the following program specification: Input: An integer n > 0 and an array A[0..(n - 1)] of n integers. Output: The smallest index s such that A[s] is the largest value in A[0..(n - 1)]. For example, if n = 9 and A = [ 4, 8, 1, 3, 8, 5, 4, 7, 2 ] (so A[0] = 4, A[1] = 8, etc.), then the program would return 1, since the largest value in...