Java Assignment: Loops, Classes, Methods, and Variables

Verified

Added on  2020/05/11

|4
|985
|401
Homework Assignment
AI Summary
This Java assignment presents several code snippets demonstrating fundamental programming concepts. It includes a `printMonth` method using a `switch` statement, and implementations of `oddEvens` using both `for` and `while` loops. The document also contrasts the characteristics of `for`, `while`, and `do-while` loops. Furthermore, it contains examples of getter and setter methods within an `Account` class, along with inheritance examples involving `Loan` and `CreditCard` classes. A `LoanTest` class with a unit test is provided. The assignment also explains variable scope, differentiating between static, instance, and local variables, and addresses compilation errors related to variable scope and declaration. The provided code showcases practical Java programming techniques and best practices.
Document Page
1. public void printMonth(int month){
switch(month){
case 1: System.out.print("the month is January");
break;
case 2: System.out.print("the month is February");
break;
case 3: System.out.print("the month is March");
break;
case 4: System.out.print("the month is April");
break;
case 5: System.out.print("the month is May");
break;
case 6: System.out.print("the month is June");
break;
case 7: System.out.print("the month is July");
break;
case 8: System.out.print("the month is August");
break;
case 9: System.out.print("the month is September");
break;
case 10: System.out.print("the month is October");
break;
case 11: System.out.print("the month is November");
break;
case 12: System.out.print("the month is December");
break;
default: System.out.print("the number must be between 1 and 12");
}
}
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
2. Implementation
a. Using for loop:
public void oddEvens(int[] theNumbers){
int odds =0;
int evens = 0;
for(int i=0; i<theNumbers.length; i++){
if(theNumbers[i]%2==0){
System.out.println("it's even.");
evens += 6;
}
else{
System.out.println("it's odd.");
odds += theNumbers[i];
}
}
System.out.println("the total odds is " + odds);
System.out.println("the total evens is " + evens);
}
b. Using while loop:
public void oddEvens(int[] theNumbers){
int odds =0;
int evens = 0;
int i=0;
while(i<theNumbers.length){
if(theNumbers[i]%2==0){
System.out.println("it's even.");
evens += 6;
}
else{
System.out.println("it's odd.");
odds += theNumbers[i];
}
i++;
}
System.out.println("the total odds is " + odds);
System.out.println("the total evens is " + evens);
}
c.
S.No For Loop While Loop Do-While Loop
1. In this, condition In this, condition In this, condition
Document Page
test is done before
the execution of the
block.
test is done before
the execution of
the block.
test is done after
the execution of
the block.
2. It is a pre-test loop It is a pre-test loop It is a post-test
loop.
3. Initialization,
condition test and
updation statements
are mentioned
separately as
compared to other
statements in the
program
Initialization and
updation
statements are
written along with
the code of the
program but the
condition test is
mentioned
separately.
Initialization and
updation
statements are
written along with
the code of the
program but the
condition test is
mentioned
separately.
4. Minimum number
of iterations are 0
Minimum number
of iterations are 0
Minimum number
of iterations are 1
3.
a. Getter and Setter codes:
public class Account{
private int accountNumber;
private double balance;
public double getBalance(){return balance;
};
public void setBalance(double b){
balance = b;
};
public int getAccount(){
return accountNumber;
};
public void setAccount(int acc){
accountNumber = acc;
};
}
b. Loan Class
public class Loan extends Account{
}
CreditCard Class
public class CreditCard extends Loan{
}
c.
public class Loan extends Account{
Document Page
public void calcInt(double interest){
this.setBalance(this.getBalance()* (1+interest));
}
}
d. public class LoanTest {
public void testLoan(){
Loan loan = new Loan();
loan.setBalance(50000);
loan.calcInt(0.05);
assertTrue(loan.getBalance() == 52500);
}
}
4.
a. variable1 is a static data member of class ScopeTest and hence, the scope of this
variable is “global to the execution” of this class. The value of this variable is
shared by all the instances of the class. If one instance makes any changes in this
variable then all other instances are reflected with the same value for this variable.
variable2 is data member of class ScopeTest and hence, the scope of this variable
is limited to only once instance of the class. As the data member is private then,
only the member methods declared inside the class can use this variable to make
any modifications for the instance of the class.
variable3 is a formal parameter for the method method1, the scope of this
variable is limited inside the method as it is declared inside it.
b. Compilation error relating to highlighting variables:
1. response = "bad"
A ; is expected at the end of the statement. In java, every statement is
terminated by a terminator (;). Also, the variable response is not declared in
this scope, as variable response is declared inside the block of if, therefore, the
scope of the variable is limited to that block and is hidden inside else.
2. System.out.println(response);
In this, the variable response is undefined. As stated above, the variable
response is declared inside the block of if and hence, is hidden outside the
block of if.
chevron_up_icon
1 out of 4
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]