At Westonci.ca, we make it easy to get the answers you need from a community of informed and experienced contributors. Explore thousands of questions and answers from knowledgeable experts in various fields on our Q&A platform. Join our platform to connect with experts ready to provide precise answers to your questions in different areas.

Create a class BankAccount with 2 variables, a int balance, and a string name. Then create an instance of the BankAccount Class, change its name and balance, and print its name and balance.

Sagot :

Answer:

Follows are the code to the given question:

class BankAccount //defining a class BankAccount

{

 int balance;//defining integer variable balance  

 String name;//defining String variable name

 BankAccount()//defining default constructor

 {

 }

  BankAccount(int balance, String name)//defining parameterized constructor that accepts two parameters  

  {

     this.balance = balance;//use this to hold parameter value

     this.name = name;//use this to hold parameter value

  }

}

public class Main//defining Main class

{

 public static void main(String[] asr)//main method  

 {

  BankAccount obv = new BankAccount(1969, "Mustang");//creating class object and pass value in parameter

   System.out.println("Your name is: " + obv.name + " and " +"Your balance is: "+ obv.balance);//print value with message

 }

}

Output:

Your name is: Mustang and Your balance is: 1969

Explanation:

In this code a class "BankAccount" is declared, that  defines the two variable "name and balance" as a integer and string, and in the next step a parameterized constructor is declared, that uses this keyword to hold value.

In the main class the main method is declared and inside the method class object is created that accepts parameter value and use the print method to print its value.