Question
Given the empty method below, using if /else and a loop write code that does the following. The numbers to be tested are passed in
Given the empty method below, using if /else and a loop write code that does the following. The numbers to be tested are passed in the array theNumbers. You have to test the size of the array and implement a loop which iterates over each number in the array and tests if the number is odd or even. If its odd print out its odd and add it to the odds. If the number is evens, print out its a even and add it to the evens. After the loop is complete print out the line the total odds is followed by the odd value, the total evens is followed by the evens value. The following is an example of testing if a number is odd or even . Given a variable num of type int and a String printIt, then
If(num%2==0)
{printIt= evens}
else
{ printIt= odd}
Implement the above using a for loop
Implement the above using a while loop.
Explain the difference between a for loop, a while loop and a do while loop.
public void oddEvens(int[] theNumbers){
int odds =0;
int evens = 0;
}
20 marks
}
Given the code below
What is the different scopes of variable1, variable2 and variable3 and explain the difference. (5 marks)
There is a compilation error in the code below relating to highlighted variables. Why? (5 marks)
public class ScopeTest{
private static int variable1;
private int variable2;
public method1( int variable3){
int variable4= variable3*2;
if (variable4>25)
{variable1=12;
String response = good;
method2();
}
else
{variable1=3;
response = bad
}
System.out.println(response);
}
public void method2(int aVar){
varariable2= 5 *aVar;
}
}
5 marks
In the code snippet below, we have a class Loan that we want to accept a loan amount into a textfield and convert it from a Fahrenheit temperature to a Centigrade temperature. Assume that all the import statements and constructor are OK.
All you have to do is complete the actionPerformed method to convert Fahrenheit temperature to a Centigrade temperature and write Java statements so that it: (5 marks)
Extracts the input from the JTextfieldLoan JTextfield and puts it into a String called loan1.
Extracts the input from the JTextfieldInterestRate JTextfield and puts it into a String called irate1.
Extracts the input from the JTextfieldYears JTextfield and puts it into a String called years1
Convert loan1 into a double and put into a double variable called loan2.
Convert irate1 into a double and put into a double variable called irate2.
Convert years1 into a double and put into a double variable called years2.
Declare a double variable called payable. Calculate its value by payable = loan2 + ((loan2*irate2)*years2).
Put the result of the calculation above in JLabel RepayableAmount
public class Loan extends JFrame implements ActionListener{
private JTextfieldLoan;
private JTextfieldInterestRate;
private JTextfieldYears;
private JLabel RepayableAmount;
private JButton doIt;
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("doIt")) {
// conversion code
// Java statements
}
}
}
AABB AaBbCcDc AaBbCc AaBbCcD ===I.-.IO' EE. 11 Heading 1 TNormal TNOSpac.. Heading 2 A. | 1 .EE Paragraph Styl In the code snippet below, we have a dass Loan that we want to accept a loan amount into a textield and convert it from a Fahrenheit temperature to a Centigrade temperature. Assume that all the import statements and constructor are OK All you have to do is complete the actionPerformed method to convert Fahrenheit temperature to a centgrade temperature and write Java statements so that it: ismar Extracts the input from the Textieldloan JTextfield and puts it into a String called oan1 a. b. Extracts the input from the TextfieldinterestRate Textield and puts it into a String called irate1 Extracts the input from the TextfieldYears Textfield and puts it into a String called years1 Convert loan1 into a double and put into a double variable called loan2. Convert irate1 into a double and put into a double variable called irate2. Convert years1 into a double and put into a double variable called years2. Declare a double variable called payable. Calculate its value by payable loan2+ ((loan2"irate2) years2). Put the result of the calculation above in JLabel BepavableAmount c. d. e. f. g. h. public class Loan extends JErame implements ActionListener private JTextfieldLoan; private JTextfieldInterestRate private ITextfield Years; private JLabel RepayableAmount private JButton dolt; public void actionPerformed(ActionEvent e) if fe.getActionCommand).equals("dolt") Iconversion code Java statements
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started