Question 2. (question2.java) (25 marks) Write a class that has the following met
ID: 3607574 • Letter: Q
Question
Question 2. (question2.java) (25 marks) Write a class that has the following methods power: This method takes two integers as arguments, one representing the base and another representing the exponent. The method then uses a for loop to compute and return the result of basnnt Eg. 23-8 passwordCheck: This method takes a single String argument and returns a boolean value. The purpose of this method is to test the strength of a password. To be considered strong a password must have the following properties: -at least 8 characters in length -at least one uppercase and one lowercase letter -at least one digit If the string parameter passed to the method passes all of these tests, return true, else return false. Use your main method to report whether or not the password is strong. fix: MatlabM has a great method called fix which returns the decimal portion of a floating point number. Write a method that takes in a double and returns only the decimal portion. Eg. 3.14159 passed in would return 0.14159. There may be some rounding issues as you test your method. Don't worry about that too much but if you want to try and solve that, that's ok too Finally, write a main ) method that prompts the user to enter inputs appropriate for testing your methods. Remember, to do al input and output in your main method. Pass the input to your methods and collect the results using assignment statements. You don't need to do any input checking. Assume the user enters appropriate values. I suggest your write and test your methods one at a time. You can get part marks for the methods that work. halfLife: Radioactive decay of materials can be modeled by the equation shown below. Write a method that takes in all the necessary arguments to compute and then return the amount of material (A) after it has decayed for a period of time (t) given a half-life value (h) A.-Age Finally, write a main ) method that prompts the user to enter inputs appropriate for testing your methods. Remember, to do all input and output in your main) method. Pass the input to your methods and collect the results using assignment statements. You don't need to do any input checking. Assume the user enters appropriate values. I suggest your write and test your methods one at a time You can get part marks for the methods that work.Explanation / Answer
question2.java
/* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class question2
{
public static int power(int a, int b)
{
int result=a;
//loop with value of exponent if it is greater than 1 and multipy result with a when b>1
if(b==0)
{
return 1;
}
else if(b==1)
{
return a;
}
else if(b>1)
{
for(int i=2;i<=b;i++)
{
result=result*a;
}
return result;
}
else
{
System.out.println("enter positive exponent");
return 0;
}
}
public static boolean passwordCheck(String pass)
{
int A=0 ,a=0 ,n=0 ,l=pass.length();
//check length is less than 8
if(l<8)
return false;
else
{
//count number of uppercase, lowercase and digit values in password.
//if each of them is greater or equal to 1 then password will be correct
for(int i=0;i<l;i++)
{
if(Character.isUpperCase(pass.charAt(i)))
A++;
else if(Character.isLowerCase(pass.charAt(i)))
a++;
else if(Character.isDigit(pass.charAt(i)))
n++;
}
if(A>=1 && a>=1 && n>=1)
return true;
}
return false;
}
public static double fix (double n)
{
int x=(int)(n);
//Subtract the integer part from the double.
return (n-(double)(x));
}
public static double halfLife(double A, int t, double hl)
{
double e=2.71828;
double result=A*(Math.pow(e, -(t * Math.log(2) /hl)));
//Applied Formula for Radio-Active-Decay
return result;
}
public static void main (String[] args)
{
System.out.println("The power of 2 raised to 3 is :"+power(2,3));
String pass="aAdfdf1d3";
if(passwordCheck(pass))
{
System.out.println("The password "+pass +" is correct");
}
else
{
System.out.println("The password "+pass +" is incorrect");
}
System.out.println("The fixed value of 2.24323 is :"+fix(2.224323));
System.out.println("The Radio-Active-Decay if A0=22.1, t=2, half-life=1 is : "+halfLife(22.1,2,1));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.