+ 3

Who can help me? I don’t understand why it doesn’t work

import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); double saving = read.nextDouble(); double checking = read.nextDouble(); Account savingAcc = new SavingAcc(saving); Account checkingAcc = new CheckingAcc(checking); System.out.println(savingAcc.getIncome()); System.out.println(checkingAcc.getIncome()); } } class Account { private double amount; public Account(double amount) { this.amount = amount; } public double getAmount() { return amount; } public double getIncome() { return 0; } } class SavingAcc extends Account { public SavingAcc(double amount) { super(amount); amount = super.getAmount(); } //Переопределите метод для сберегательного счета public double getIncome() { return amount + amount * 0.2; } } class CheckingAcc extends Account { public CheckingAcc(double amount) { super(amount); amount = super.getAmount(); } //Переопределите метод для чекового счета public double getIncome() { return amount + amount * 0.05; } } }

13th Feb 2021, 6:57 PM
Natalia Zvezdina
Natalia Zvezdina - avatar
13 Respostas
+ 4
Natalia Zvezdina fixed it for you hope it all makes sense now 😊 https://code.sololearn.com/cba2UmyY6zVq/?ref=app
13th Feb 2021, 11:21 PM
D_Stark
D_Stark - avatar
+ 4
Natalia Zvezdina your welcome, super.getAmount(); is just fetching the current value of "amount" field in super.. if amount wasnt private then you could just use variable "amount" like you would any inherited variable.
13th Feb 2021, 11:43 PM
D_Stark
D_Stark - avatar
+ 3
Natalia Zvezdina if you look at the original code there was an extra brace -> } <- at the bottom of your code which I removed, this was causing the no output screen
13th Feb 2021, 11:28 PM
D_Stark
D_Stark - avatar
+ 3
You can also define protected double amount instead of private. In this way you don't need to use super.getAmount(), you can access it directly this.amount.. etc like this : https://code.sololearn.com/cmQzzS69hiWd/?ref=app
15th Feb 2021, 3:58 PM
Anon Fox
Anon Fox - avatar
+ 2
Natalia Zvezdina in programming it either works or it doesn't it never somtimes works, super class field amount is declared private so is not inherited by the derived class, So "this.amount" in constructor is wrong as amount doesn't exist in CheckingAcc due to private access in superClass You shouldn't really need to create another variable for amount as you can retrieve it through getters and setters which makes your code secure. so my advice is remove amount in extending classes and replace returns with this..... return super.getAmount+super.getAmount*5;
13th Feb 2021, 10:44 PM
D_Stark
D_Stark - avatar
+ 2
what’ the ***uuuck. i wrote the same, but my sololearn app doesn’t output and your programm output all wright)))
13th Feb 2021, 11:26 PM
Natalia Zvezdina
Natalia Zvezdina - avatar
+ 2
ooh, yes. Now it works. Thank you a lot. but i don’t understand why should i only write super.getAmount() in return ?
13th Feb 2021, 11:35 PM
Natalia Zvezdina
Natalia Zvezdina - avatar
+ 1
when i define a new one in subclasses it doesn’t work all the same
13th Feb 2021, 7:39 PM
Natalia Zvezdina
Natalia Zvezdina - avatar
+ 1
Use a different name then.. What not working? What is your expected output for a sample input..? or Define new one and use this.amount=super.getAmount();
13th Feb 2021, 7:51 PM
Jayakrishna 🇮🇳
+ 1
thank you for help, but i’ve replaced return and programm doesn’t output anything. it seem to me that somethhing more wrong is with extending classes
13th Feb 2021, 11:13 PM
Natalia Zvezdina
Natalia Zvezdina - avatar
+ 1
import java.util.Scanner; class Main { public static void main(String[] args) { Scanner read = new Scanner(System.in); double saving = read.nextDouble(); double checking = read.nextDouble(); Account savingAcc = new SavingAcc(saving); Account checkingAcc = new CheckingAcc(checking); System.out.println(savingAcc.getIncome()); System.out.println(checkingAcc.getIncome()); } } class Account { double amount; public Account(double amount) { this.amount = amount; } public double getAmount() { return amount; } public double getIncome() { return 0; } } class SavingAcc extends Account { public SavingAcc(double amount) { super(amount); } //Переопределите метод для сберегательного счета public double getIncome() { amount = amount + amount * 0.2; return amount; } } class CheckingAcc extends Account { public CheckingAcc(double amount) {
11th Jan 2022, 1:32 PM
Максим Малов
Максим Малов - avatar
+ 1
It simply, then u think. But don't forget about access double amount in class Account
11th Jan 2022, 1:33 PM
Максим Малов
Максим Малов - avatar
0
The amount variable in Account class has private access so you cant use it derived or sub classes... so define a new one in subclasses..
13th Feb 2021, 7:04 PM
Jayakrishna 🇮🇳