#new to classes need help
1 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @fresh laurel! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
hi
thats my code and idk why i cant run it
i have made a method to take input in the bank class
still the code cant run when i try to run it
import java.util.*;
class methods {
String name;
int accnumber;
Scanner sc = new Scanner(System.in);
public void takein(){
System.out.println("please enter your name: ");
name = sc.nextLine();
System.out.println("please enter your account number: ");
accnumber = sc.nextInt();
}
public void displayname() {
System.out.println("your name is " + name);
}
public void displayaccnumber() {
System.out.println("your account number is: " + accnumber);
}
public void displayall() {
System.out.println("your name is" + name);
System.out.println("your account number is " + accnumber);
}
public class bank{
public static void main(String args[]){
methods customer1 = new methods();
customer1.takein();
}
}
}
which line is the error on
Usually a program starts from a method called main
you currently have chosen current file to start execution
it can't run it since there is no main method inside the file you showed
there is
class methods also wants main ?
oh shit I missed it above. My bad.
and when you are in the file where this is you should be able to run it
usually you'll have only 1 class in 1 file so create a new file and move it there
(just a tip. the classes names are accepted to be in pascal case)
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
thank you
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
and apologies my mom called me and there was quite some work that she gave me which is why i've been out all day
oh if thats the case how does inheritance work
Independently.
Typically a class will inherit from another, and both classes will be in different files.
The class that inherits from the other is said to extend the other, and it will be declared class Stuff extends Thing { instead of just class Stuff {
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
ohh is that the case - i see so now i have to make different files but but how will my intellij idea know that im extending to a class which is in a different file ? will i have to import that file ?
IntelliJ has a fairly good idea of how Java works, and Java finds other classes from the fact that they're in the same directory.
When working with classes in different directory, the mechanism is a little more complicated and yes, it involves importing. Though you import class paths, not files.
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
i am tying to make a bank model i'll keep this open
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
import java.util.Random;
import java.util.Scanner;
interface BankInterface {
void deposit();
void withdraw();
}
class Methods implements BankInterface {
String name;
int accnumber;
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int bb = rand.nextInt(1, 1000000);
int math;
int w;
public void takein() {
System.out.print("Please enter your name: ");
name = sc.nextLine();
System.out.print("Please enter your account number: ");
accnumber = sc.nextInt();
}
public void displayname() {
System.out.println("Your name is " + name);
}
public void displayaccnumber() {
System.out.println("Your account number is: " + accnumber);
}
public void displayall() {
System.out.println("Your name is " + name);
System.out.println("Your account number is " + accnumber);
}
public void bankbalance() {
System.out.println("Your bank balance is: ₹" + bb);
}
@Override
public void deposit() {
int x;
System.out.print("Enter the amount of money would you like to deposit:");
x = sc.nextInt();
math = x + bb;
System.out.println("Your new bank balance is: " + math);
}
@Override
public void withdraw() {
int x;
System.out.print("Enter the amount of money would you like to withdraw: ");
x = sc.nextInt();
math = bb - x;
System.out.println("Your new bank balance is: " + math);
}
}
thats my code for my methods class
and i have bank class which extends this class^
bank class calls methods that i have implemented in this class
the problem here is
when a customer enters their name and bank number my code asks them do they wanna view their balance , add money to it or withdraw money
the initial balance is randomised
and its called "bb" in my code
now lets say customer has 20k amount already and adds another 20k by deposit function
and goes ahead and deletes 10k out of it
the program is written that it should remove 10k out of the initial balance which was 20k
so it makes it 10k
but logically - a correctly written program should make it 20k + 20k -10k = 30k
so i am here to gather some ideas about how can i make it such that it knows if customer deposited some money in bank then bb should change and according to the new balance it should gimme the next calculation
also i'd like to add another thing which is a customer shouldnt be able to withdraw more than he already has in the bank
so the figure shouldnt go less than 0 and then print an error message which says insufficient balance
any ideas ?
okay i managed to fix this issue IT WAS SO EASY
i had to remove math from there and yeah update bb itself
this one however still eating my brain
First compute what the resulting balance should be in theory
Then check whether that balance is lower than zero
If it is, then withdrawal is impossible
good thanks imma do that real quick
If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.
If it is zero or more, the operation is valid and you can update the real balance with the candidate value
Note that that was an engineering response.
A layman person would estimate that they can't withdraw more than they have, so they'd check if the requested withdrawal amount is greater than their balance
no thats true but my teacher could try to mess with me by withdrawing more than i have in bank while i show him my code and if he does that i want to have an answer
so im doing it beforehand
import java.util.Random;
import java.util.Scanner;
interface BankInterface {
void deposit();
void withdraw();
}
class Methods implements BankInterface {
String name;
int accnumber;
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int bb = rand.nextInt(1, 1000000);
int math;
int w;
public void takein() {
System.out.print("Please enter your name: ");
name = sc.nextLine();
System.out.print("Please enter your account number: ");
accnumber = sc.nextInt();
}
public void displayname() {
System.out.println("Your name is " + name);
}
public void displayaccnumber() {
System.out.println("Your account number is: " + accnumber);
}
public void displayall() {
System.out.println("Your name is " + name);
System.out.println("Your account number is " + accnumber);
}
public void bankbalance() {
System.out.println("Your bank balance is: ₹" + bb);
}
@Override
public void deposit() {
int x;
System.out.print("Enter the amount of money would you like to deposit:");
x = sc.nextInt();
bb = x + bb;
System.out.println("Your new bank balance is: " + bb);
}
@Override
public void withdraw() {
int x;
System.out.print("Enter the amount of money would you like to withdraw: ");
x = sc.nextInt();
bb = bb - x;
if (bb < 0) {
System.out.println("YOU CANT WITHDRAW MORE THAN YOU HAVE");
System.exit(0);
} else if (bb >= 0) {
System.out.println("Your new bank balance is: " + bb);
}
}
}
yep i've done it
now gotta make a switchcase in the mainclass to run these methods
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
In case your post is not getting any attention, you can try to use/help ping.
Warning: abusing this will result in moderative actions taken against you.
up
hi
import java.util.Random;
import java.util.Scanner;
interface BankInterface {
void deposit();
void withdraw();
}
class Methods implements BankInterface {
String name;
int accnumber;
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int bb = rand.nextInt(1, 1000000);
public void takein() {
System.out.print("Please enter your name: ");
name = sc.nextLine();
System.out.print("Please enter your account number: ");
accnumber = sc.nextInt();
}
public void bankbalance() {
System.out.println("Your bank balance is: ₹" + bb);
}
@Override
public void deposit() {
int x;
System.out.print("Enter the amount of money would you like to deposit:");
x = sc.nextInt();
bb = x + bb;
System.out.println("Your new bank balance is: ₹" + bb);
}
@Override
public void withdraw() {
int x;
System.out.print("Enter the amount of money would you like to withdraw: ");
x = sc.nextInt();
bb = bb - x;
if (bb < 0) {
System.out.println("YOU CANT WITHDRAW MORE THAN YOU HAVE");
System.exit(0);
} else if (bb >= 0) {
System.out.println("Your new bank balance is: ₹" + bb);
}
}
public void displayall() {
System.out.println("Your name is " + name);
System.out.println("Your account number is " + accnumber);
System.out.println("your bank balance is: " + bb);
}
}
import java.util.Scanner;
public class bank extends Methods {
public static void main(String[] args) {
Methods customer1 = new Methods();
customer1.takein();
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("1. Display Bank Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Display All Information");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
customer1.bankbalance();
break;
case 2:
customer1.deposit();
break;
case 3:
customer1.withdraw();
break;
case 4:
customer1.displayall();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("Invalid choice. Please try again.");
break;
}
} while (choice != 0);
}
}
i went ahead and made some changes in my code
`Please enter your name: Aarpan
Please enter your account number: 108289
- Display Bank Balance
- Deposit Money
- Withdraw Money
- Display All Information
- Exit
Enter your choice: 1
Your bank balance is: ₹383696 - Display Bank Balance
- Deposit Money
- Withdraw Money
- Display All Information
- Exit
Enter your choice: 2
Enter the amount of money would you like to deposit:4
Your new bank balance is: ₹383700 - Display Bank Balance
- Deposit Money
- Withdraw Money
- Display All Information
- Exit
Enter your choice: 3
Enter the amount of money would you like to withdraw: 700
Your new bank balance is: ₹383000 - Display Bank Balance
- Deposit Money
- Withdraw Money
- Display All Information
- Exit
Enter your choice: 4
Your name is Aarpan
Your account number is 108289
your bank balance is: 383000 - Display Bank Balance
- Deposit Money
- Withdraw Money
- Display All Information
- Exit
Enter your choice: 0
Exiting...
`
thats the output it gives
im pretty satisfied with it
but
i want to underline or somehow highlight the printed output
We all want things, but before Windows 11, Windows has not been playing nice, and that make things unreliable
ah thats sad
is there any other way you can recommend ? to increase the visibilty of the output
because the text really kinda eats it up
Lookup terminal escape codes and try something like
System.out.println("\u001b[31mHelloWorld");
Otherwise you could add newlines and draw boxes with # characters
right
this makes it all red somehow
So it works. Lookup all other possible codes
but it happens only when
for eg
import java.util.Scanner;
public class bank extends Methods {
public static void main(String[] args) {
Methods customer1 = new Methods();
customer1.takein();
Scanner sc = new Scanner(System.in);
int choice;
do {
System.out.println("1. Display Bank Balance");
System.out.println("2. Deposit Money");
System.out.println("3. Withdraw Money");
System.out.println("4. Display All Information");
System.out.println("0. Exit");
System.out.print("Enter your choice: ");
choice = sc.nextInt();
switch (choice) {
case 1:
customer1.bankbalance();
break;
case 2:
customer1.deposit();
break;
case 3:
customer1.withdraw();
break;
case 4:
customer1.displayall();
break;
case 0:
System.out.println("Exiting...");
break;
default:
System.out.println("\u001b[31mHelloWorld");
;
break;
}
} while (choice != 0);
}
}
look at the last lines
System.out.println("Invalid choice. Please try again.");
i replaced these lines with the thing u gave me
and when i type 20 inside the switch it then makes it red
otherwise its white
Yes, my friend, using this thing requires a complicated organization. If you want to be a programmer, start making do with that
right
Verification
what
@errant urchin
import java.util.Random;
import java.util.Scanner;
interface BankInterface {
void deposit();
void withdraw();
}
class Methods implements BankInterface {
String name;
int accnumber;
Scanner sc = new Scanner(System.in);
Random rand = new Random();
int bb = rand.nextInt(1, 1000000);
public void takein() {
System.out.print("Please enter your name: ");
name = sc.nextLine();
System.out.print("Please enter your account number: ");
accnumber = sc.nextInt();
}
public void bankbalance() {
System.out.println("Your bank balance is: ₹" + bb);
}
@Override
public void deposit() {
int x;
System.out.print("Enter the amount of money would you like to deposit:");
x = sc.nextInt();
bb = x + bb;
System.out.println("Your new bank balance is: ₹" + bb);
}
@Override
public void withdraw() {
int x;
System.out.print("Enter the amount of money would you like to withdraw: ");
x = sc.nextInt();
if (bb < 0) {
System.out.println("INSUFFICIENT FUNDS. CANNOT WITHDRAW MORE THAN YOUR BALANCE.");
} else {
bb = bb - x;
System.out.println("Your new bank balance is: ₹" + bb);
}
}
public void displayall() {
System.out.println("Your name is " + name);
System.out.println("Your account number is " + accnumber);
System.out.println("your bank balance is: " + bb);
}
}
i need to use abstract class here
can you help me think how
Errm. I struggle to imagine a smart reason to use abstract classes. Why do you need to?
wasnt letting me in unless i sent a message
uhhh my internet was acting up
actually this is my college projec
and i have to use some topics
class
interface
switch before main class
methods
abstract class
access modifiers :
constructors this keyword
scanner
array
static blocks
variables (static instance methods)
object
packages
STRING (wrapper cladd loader)
data types (char double float)
comments
use of loop
method overloading n overriding
inheritance
super()
keywords ....polymorphism
these
I'm afraid your project might be way too simple to go into much subjects
I'm not super inspired. Maybe with some different kinds of interest plans that have completely different formulas