public class Account{ private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } } public class Main{ public static void main(String args[]) { Account accObj1=new Account(1001,"Chandru",50000); System.out.println(accObj1); }}If the above code should give the output as “Account Number : 1001, Holder : Chandru, Balance : 50000”, which method should be overridden and that method should be written in which class.Select one:a.Override toString() method in Account classb.Override tostring() method in Account classc.Override tostring() method in Main classd.Override toString() method in Main class
Question
public class Account{ private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } } public class Main{ public static void main(String args[]) { Account accObj1=new Account(1001,"Chandru",50000); System.out.println(accObj1); }}If the above code should give the output as “Account Number : 1001, Holder : Chandru, Balance : 50000”, which method should be overridden and that method should be written in which class.Select one:a.Override toString() method in Account classb.Override tostring() method in Account classc.Override tostring() method in Main classd.Override toString() method in Main class
Solution
The correct answer is:
a. Override toString() method in Account class
The toString() method is a built-in method in Java that returns a string representation of an object. By default, it returns the class name followed by '@' and the hashcode of the object. If you want to provide a meaningful string representation of an object (like in this case), you need to override this method in the class of the object.
Here is how you can do it in the Account class:
@Override
public String toString() {
return "Account Number : " + accountNumber + ", Holder : " + holderName + ", Balance : " + balance;
}
This will ensure that when you print an Account object using System.out.println(), it will print the string returned by the toString() method instead of the default string representation.
Similar Questions
public class Main{ public static void main(String args[]) { Account accObj1=new Account(1001,"Chandru",50000); Account accObj2=new Account(1001,"Chandru",50000); //Invoke the equals method for the above two objects. System.out.println(.()); }}
public class Employee {private int id;private String name;private float salary;private String pancardno;public Employee(){System.out.println("In default constructor");}public Employee(int id, String name, float salary) {this.id = id;this.name = name;this.salary = salary;}public Employee(int id, String name, float salary, String pancardno) {this(id,name,salary); this.pancardno = pancardno; }this();}
// BankAccount.java// Parent class BankAccountpublic class BankAccount { private String accountNumber; private double balance; public BankAccount(String accountNumber, double balance) { this.accountNumber = accountNumber; this.balance = balance; } public void deposit(double amount) { balance += amount; } public void withdraw(double amount) { if (balance >= amount) { balance -= amount; } else { System.out.println("Insufficient balance"); } } public double getBalance() { return balance; }}Copy// SavingsAccount.java// Child class SavingsAccountpublic class SavingsAccount extends BankAccount { public SavingsAccount(String accountNumber, double balance) { super(accountNumber, balance); } @Override public void withdraw(double amount) { if (getBalance() - amount < 100) { System.out.println("Minimum balance of $100 required!"); } else { super.withdraw(amount); } }}Copy // Main.java// Main classpublic class Main { public static void main(String[] args) { System.out.println("Create a Bank Account object (A/c No. BA1234) with initial balance of $500:"); //Create a BankAccount object (A/c No. "BA1234") with initial balance of $500 BankAccount BA1234 = new BankAccount("BA1234", 500); // Deposit $1000 into account BA1234 System.out.println("Deposit $1000 into account BA1234:"); BA1234.deposit(1000); System.out.println("New balance after depositing $1000: $" + BA1234.getBalance()); // Withdraw $600 from account BA1234 System.out.println("Withdraw $600 from account BA1234:"); BA1234.withdraw(600); System.out.println("New balance after withdrawing $600: $" + BA1234.getBalance()); // Create a SavingsAccount object (A/c No. "SA1234") with initial balance of $450 System.out.println("\nCreate a SavingsAccount object (A/c No. SA1234) with initial balance of $450:"); SavingsAccount SA1234 = new SavingsAccount("SA1234",450); // Withdraw $300 from SA1234 SA1234.withdraw(300); System.out.println("Balance after trying to withdraw $300: $" + SA1234.getBalance()); // Create a SavingsAccount object (A/c No. "SA1000") with initial balance of $300 System.out.println("\nCreate a SavingsAccount object (A/c No. SA1000) with initial balance of $300:"); SavingsAccount SA1000 = new SavingsAccount("SA1000",300); // Withdraw $250 from SA1000 (balance falls below $100) System.out.println("Try to withdraw $250 from SA1000!"); SA1000.withdraw(250); System.out.println("Balance after trying to withdraw $250: $" + SA1000.getBalance()); }}
public class Test { public void m1(String arg1) { arg1="Test"; } public static void main(String[] args) { Test test1= new Test(); String s= "Hello"; test1.m1(s); System.out.println(s); }}
Drag and drop the apt option in the space provided to get the output as “Account Number : 1001, Holder : Chandru, Balance : 50000”.Code :public class Account { private int accountNumber; private String holderName; private double balance; public Account(int accountNumber, String holderName, double balance) { this.accountNumber = accountNumber; this.holderName = holderName; this.balance = balance; } //Override toString method blank blank blank { blank "Account Number : "+accountNumber+", Holder : "+holderName +", Balance : "+balance; }}public class Main { public static void main(String args[]) { Account accObj1=new Account(1001,"Chandru",50000); blank ( blank ); }} System.out.println public toString( ) return String accObj1
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.