Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

STAGE 1 | (Decimal to Binary) Write a recursive method that converts a decimal n

ID: 3830642 • Letter: S

Question

STAGE 1 | (Decimal to Binary)

Write a recursive method that converts a decimal number into a binary number as a string. The method header is: ** DO NOT USE ANY SPECIAL METHODS **

public static String decimalToBinary(int value)

The non-recursive version is:

public static String decimalToBinary(int value)

{

   String s="";

   while (value != 0)

   {

     s = (value % 2) + s;

     value /= 2;

   }

   return s;

  }

STAGE 2 | (testing)

Test your code with following:

import java.util.Scanner;

public class Lab21_01 {

public static void main(String[] args) {

   Scanner input = new Scanner(System.in);

   System.out.print("Enter a decimal integer: ");

   int decimal = input.nextInt();

   System.out.println(decimal + " is binary " + decimalToBinary(decimal));

     }

// Complete the implementation

public static String decimalToBinary(int value) {

  

}

}



STAGE 3 | (String matching)

Use recursion to implement a method that checks to see if a string contains a given substring.

The method header is:

public static boolean find(String str, String match)

For example:

boolean b = find(“Mississippi”, “sip”); // sets to be true

Hint: If str length < match length, then return false. If str starts with the match, then you are done (check str and match character by character to see if all characters are the same). If not, consider the string that you obtain by removing the first character. Matching is not case sensitive.

STAGE 4 | (testing)

Test your code with following:

public class Lab21_02 {

  public static void main(String[] args) {

     System.out.println("Mississippi contains sip is " + find("Mississippi","sip"));

     System.out.println("Mississippi contains pip is " + find("Mississippi","pip"));

     System.out.println("Mississippi contains pip is " + find("Mississippi","miss"));

  

  }

  

  // Implement the method

  public static boolean find(String str, String match)

  {

  

  }

     

}







STAGE 5 | (The Account class)

Design a class named Account that contains:

A private int data field named id for the account (default 0).

A private double data field named balance for the account (default 0).

A private double data field named annualInterestRate that stores the current interest rate (default 0). Assume all accounts have the same interest rate.

A private Date data field named dateCreated that stores the date when the account was created.

A no-arg constructor that creates a default account.

A constructor that creates an account with the specified id and initial balance.

The accessor and mutator methods for id, balance, and annualInterestRate.

The accessor method for dateCreated.

A method named getMonthlyInterestRate() that returns the monthly interest rate.

A method named getMonthlyInterest() that returns the monthly interest.

A method named withdraw that withdraws a specified amount from the account. (Error checking: insufficient fund)

A method named deposit that deposits a specified amount to the account.

A method named toString() that prints the account id, balance, the monthly interest, and the date account was created.

(Hint:)

The method getMonthlyInterest() is to return monthly interest, not the interest rate.

Monthly interest is balance * monthlyInterestRate.

monthlyInterestRate is annualInterestRate / 12.

Note that annualInterestRate is a percentage, e.g., like 4.5%. You need to divide it by 100.)

STAGE 6 | (Driver Program)

Write a test program that creates an Account object with an account ID of 1122,

a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw

method to withdraw $2,500, use the deposit method to deposit $3,000, and print

the balance, the monthly interest, and the date when this account was created.

Explanation / Answer

import java.util.Scanner;

public class Lab21_01 {

  

   // Complete the implementation

   public static String decimalToBinary(int value) {

         

       String bin = String.valueOf(value % 2 == 0 ? 0:1);

          if (value == 0 || value == 1) {

          return bin;

          }

          return decimalToBinary(value/2) + bin;

  

   }

public static void main(String[] args) {

   Scanner input = new Scanner(System.in);

   System.out.print("Enter a decimal integer: ");

   int decimal = input.nextInt();

   System.out.println(decimal + " is binary " + decimalToBinary(decimal));

   }

}
==============
See Output


Thanks, let me know if there is any concern.