Bunch of errors and IDK why [closed] - java

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed last year.
Improve this question
I am a new coder, and have no clue why im getting a bunch of errors in this program im trying to make? can someone show me where I went wrong? I think im having issues with figuring out constructors and classes. I think my methods are correctly formatted, but I may be wrong.
public class Bank
{
public static void main(String[] args)
{
private double balance;
private String name;
private int pin;
public void Bank(double balance, String name, int pin)
{
this.balance = balance;
this.name = name;
this.pin = pin;
}
public String withdraw(double amount)
{
if(balance - amount >= 0)
{
double val = (double)(balance - amount);
String value = String.valueOf(val);
return value;
}
else
{
return " insufficient funds";
}
}
public String getName()
{
return name;
}
public int getPin()
{
return pin;
}
//commented out
//System.out.println("Succesfully excecuted. ");
}
}

You are keeping your instance variables, constructor and other method inside the main method. You need to keep them outside the main block but inside the class. You can call a method from another method but you cannot declare a method inside another method.
public class Bank
{
private double balance;
private String name;
private int pin;
public void Bank(double balance, String name, int pin)
{
this.balance = balance;
this.name = name;
this.pin = pin;
}
public static void main(String[] args)
{
// create object and perform operation
//commented out
//System.out.println("Succesfully excecuted. ");
}
public String withdraw(double amount)
{
if(balance - amount >= 0)
{
double val = (double)(balance - amount);
String value = String.valueOf(val);
return value;
}
else
{
return " insufficient funds";
}
}
public String getName()
{
return name;
}
public int getPin()
{
return pin;
}
}

Variable with modifiers are defined at class level not a method level. Paste this code outside the main method and you're good to go.
Also this is my first answer here :)

Related

Linking Credit Card and Debit Card classes to a Bank Account Class

I'm learning Java, and one of my assignments is to create Credit and Debit Cards classes so that you can create new cards linked to a new individual account when one is created. The credit card should store purchases, and when the user makes a payment (total or partial), substract the amount from the account (plus 3% interest). The debit card immediately substracts the purchase's amount from the account.
I have written everything and made it work, but only as nested classes within the Account class. Is there a way to have Account, Credit and Debit as three separate classes and make it so that every time that you create a new account, you can optionally create cards linked to each new individual account?
Disclaimer: I'm trimming some of the code because it's all in spanish so I don't have to translate it all (mainly booleans to check for positive amounts and stuff like that), but the code is still functional. Thanks a lot in advance!
package accounts;
public class Account {
protected static double balance;
protected static String accountNumber;
public Account() {
}
public Account(String accountNumber, double balance) {
Account.accountNumber = accountNumber;
Account.balance = balance;
}
public double getBalance() {
return balance;
}
public String getAccountNumber() {
return accountNumber;
}
public void deposit(double depositAmount) {
Account.balance += depositAmount;
}
public void extraction(double extractionAmount) {
Account.balance -= extractionAmount;
}
public void showBalance() {
System.out.println("Your current balance is: " + getBalance());
}
protected static class DebitCard {
private String userName;
private int cardNumber;
public DebitCard () {
}
public DebitCard (String userName, int cardNumber) {
this.userName = userName;
this.cardNumber = cardNumber;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public void debitPurchase(double purchaseAmount) {
Account.balance -= purchaseAmount;
}
}
protected static class CreditCard {
private String userName;
private int cardNumber;
private double creditCardDebt;
private double limit;
public CreditCard () {
}
public CreditCard(String userName, int cardNumber, double limit) {
this.userName = userName;
this.cardNumber = cardNumber;
this.limit = limit;
}
public String getUserName() {
return userName;
}
public int getCardNumber() {
return cardNumber;
}
public double getLimit() {
return limit;
}
public double getCrediCardDebt() {
return creditCardDebt;
}
public void creditCardPurchase(double purchaseAmount) {
if (this.creditCardDebt + purchaseAmount > this.limit) {
Error notEnoughLimit = new Error("There's not enough limit to make this purchase");
throw notEnoughLimit ;
} else {
this.creditCardDebt += purchaseAmount + (purchaseAmount * 0.03);
this.limit -= purchaseAmount;
this.creditCardDebt += purchaseAmount + (purchaseAmount* 0.03);
this.limit -= purchaseAmount;
}
}
public void payCard(double payAmount) {
Account.balance -= payAmount;
this.creditCardDebt = this.creditCardDebt - payAmount;
}
}
}
Yes it is possible. But before you do that, you should solve the problem caused by declaring balance and accountNumber as static variables. By doing that, you have made every Account instance share one account number and one balance.
Those variables should also be private so that other classes can't access or change them directly.
Once you have fixed that, you should then change the constructors for Credit and Debit to take an instance of Account as a parameter. They store this in a private field, and then perform operations to add and remove money from the Account ... via operations on the Account object.

Java: how to pass a variable from one class to another using access modifiers

So I'm brand new to java, and have experience doing this in C# but something seems different. I am trying to make the input from the user be passed to the set method for the variable in another class.
//this is the class im trying to pass the variable to
public class HealthProfile
{
private String name="";
private double age;
private double weight;
private double height;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAge() {
return age;
}
public void setAge(double age) {
this.age = age;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
}
// this is the method im trying to pass the value from taking the users input
import java.util.Scanner;
public class Lab1Main {
public void getInput()
{
HealthProfile user = new HealthProfile();
Scanner input = new Scanner(System.in);
System.out.print("Please enter your name: ");
user.setName(input.nextLine());
System.out.print("Your Age: ");
user.setAge(input.nextDouble());
System.out.print("Your Weight: ");
user.setWeight(Double.parseDouble(input.nextLine()));
}
}
Wrong syntax:
user.setName(aName)= input.nextLine();
should be
user.setName(input.nextLine());
for example. In Java, there is no such thing as a "named parameter"; when you call methods, you simply have to provide the parameters simply "in place", in the exact same order as specified in the method declaration.
And a word on the title of your question: access modifiers do only control which fields/methods can be accesses from other classes; you cannot "use" them to pass anything.

hashcode appearing instead of int, Object name not appearing correctly? [duplicate]

This question already has answers here:
How do I print my Java object without getting "SomeType#2f92e0f4"?
(13 answers)
Closed 6 years ago.
As part of my project, I have to make a loop to repeatedly add objects to my queue
Here is the code
for(int rabbitcounter=0; rabbitcounter<30;rabbitcounter++) {
yettoracequeue.add(new Rabbits("Rabbit" + rabbitcounter, Math.random()*100));
System.out.println(yettoracequeue);
System.out.println(rabbitcounter);
I always use System.out.println to check if things are going as expected.
However when the System.out.println executes above, it gives me
[queuepart.Rabbits#7852e922]
Instead of Rabbit 1.
Using the above, I tried to call the getName() method from my Rabbits class on it with the following line
System.out.println(queuepart.Rabbits#7852e922.getName());
but it gives at error. From what I understand it is because the object has not been initialized.
Here is my Rabbits class
package queuepart;
public class Rabbits {
// properties of rabbits
private double speed;
private String name;
//private int counter = 1;
//Constructor, must be name of object
public Rabbits() {
}
public Rabbits(String name, double speed) {
this.name = name;
this.speed = speed;
//counter++;
}
//Speedgetter
public double getSpeed() {
return speed;
}
//Namegetter
public String getName() {
return name;
}
//Speedsetter
public void setSpeed (double speed) {
this.speed = speed;
}
//Namesetter
public void setName(String name) {
this.name = name;
}
}
I think I am still able to proceed to the next step of my project with the wrongly provided names but the conclusion of my project requires me to have the correct rabbit names e.g Rabbit 1, Rabbit 2 etc etc
Thank you and sorry for the long post. :)
You should override toString() method in your Rabbits class
#Override
public String toString() {
return name;
}
Override the toString() method of Object class. On you Rabbits class add
#Override
public String toString(){
return "[Rabbit name: " + this.name + " - Rabbit speed: " + this.speed + " ]";
}
Change your Rabbit class so as to override the toString() methos from Object class as below:
package queuepart;
public class Rabbits {
// properties of rabbits
private double speed;
private String name;
//private int counter = 1;
//Constructor, must be name of object
public Rabbits() {
}
public Rabbits(String name, double speed) {
this.name = name;
this.speed = speed;
//counter++;
}
//Speedgetter
public double getSpeed() {
return speed;
}
//Namegetter
public String getName() {
return name;
}
//Speedsetter
public void setSpeed (double speed) {
this.speed = speed;
}
//Namesetter
public void setName(String name) {
this.name = name;
}
public String toString(){
return this.name + " " + this.speed;
}
}

Java compiling error on Eclipse

So I'm working on this program and I've created two classes, one class called CreditCard, the other called CreditCardTester.I'm using Eclipse IDE and I keep getting compiling errors such as "The method getBalance(double) in the type CreditCard is not applicable for the arguments ()". I'm not really sure on what I have to fix.
This is the first class called CreditCard:
public class CreditCard
{
private String accountNumber;
private double creditLimit;
private double balance;
public void CreditCard(String number, double limit)
{
accountNumber = number;
limit = creditLimit;
balance = 0;
}
public String getAccountNumber()
{
return accountNumber;
}
public double getCreditLimit()
{
return creditLimit;
}
public double getBalance(double theBalance)
{
return balance;
}
public double charge(double amount)
{
balance = balance + amount;
return balance;
}
This is the second class CreditCardTester:
public class CreditCardTester
{
public static void main(String[] args)
{
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
String formatString = "Credit Card [number = %s, bal = %.2f, limit = %.2f]\n";
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
System.out.println("\nCharging $50.00 to credit card...\n");
card.charge(50.00);
System.out.printf(formatString, card.getAccountNumber(), card.getBalance(), card.getCreditLimit());
}
This is not a constructor, because you added void, making it a normal method:
public void CreditCard(String number, double limit)
Remove void:
public CreditCard(String number, double limit)
Also, one of the assignments in the method/constructor is backwards. You assigned the instance variable to the parameter.
limit = creditLimit;
Change it around:
creditLimit = limit;
You're missing "new" when creating a CreditCard:
CreditCard card = CreditCard("1234-5678-9012-3456", 1000.00);
Try
CreditCard card = new CreditCard("1234-5678-9012-3456", 1000.00);
You have an unused parameter on the getBalance method, and you call it without a parameter.
public double getBalance(double theBalance)
Remove it:
public double getBalance()
I think getBalance should not accept an argument in your class definition.
Use this in your CreditCard class:
public double getBalance()
{
return balance;
}

How to create method for withdrawal from store and deliverance to store

I am studying java by myself and I want to get help on exercise which i am doing myself.
The class is called Product which used for representing a product that a small company sells.
It should be possible to store the following information about each product.
The class should have the following methods:
A constructor
A method that returns the units of items in store
A method for deliverance to the store (increases the units of this product)
A method for withdrawal from the store (decreases the units of this product)
Please note that if one of the methods changes the stored items below the order point a message should be printed. It should also be impossible to have a negative amount of items.
I HAVE PROBLEM WITH METHODS. PLEASE TAKE A LOOK MY CODE AND GIVE ME SOME HINTS. I WILL APPRECIATE ALL RESPONDS.
THANK YOU.
Here is my program:
public class Product {
private int productNumber;
private String productName;
private float price;
private int orderPoint;
private int unitsInStore;
private String proDescription;
public Product(int num, String name, float price, int order, int units, String description){
this.productNumber = num;
this.productName = name;
this.price = price;
this.orderPoint = order;
this.unitsInStore = units;
this.proDescription = description;
}
public int getProductNumber() {
return productNumber;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getOrderPoint() {
return orderPoint;
}
public void setOrderPoint(int orderPoint) {
this.orderPoint = orderPoint;
}
// a method returns the units in store
public int getUnitsInStore() {
return unitsInStore;
}
public void setUnitsInStore(int unitsInStore) {
this.unitsInStore = unitsInStore;
}
public String getProDescription() {
return proDescription;
}
public void setProDescription(String proDescription) {
this.proDescription = proDescription;
}
public int deliveranceToStore(int store){
unitsInStore = unitsInStore + store;
return unitsInStore ++ ;
}
public int withdrawal(int store){
unitsInStore = store - unitsInStore;
return unitsInStore --;
}
}
The deliveranceToStore method isn't correct. Why are you calling the method recursively?
The method can simply be:
public int deliveranceToStore(int store) {
unitsInStore = unitsInStore + store;
return unitsInStore;
}
If there is no need to return the number of units in store with this call, you should have the return type as void (i.e., if updating the count is sufficient):
public void deliveranceToStore(int store) {
unitsInStore = unitsInStore + store;
}
For withdrawal, you need a similar strategy where unitsInStore is updated:
public void withdrawal(int units) {
if(unitsInStore - units >= 0) {
unitsInStore = unitsInStore - units;
} else {
System.out.println("Unable to withdraw. Insufficient units in store.");
}
}
You can also make the withdrawal method return a boolean which tells whether the withdrawal action was successful. The method, in that case, may look like:
public boolean withdrawal(int units) {
if(unitsInStore - units >= 0) {
unitsInStore = unitsInStore - units;
return true;
} else {
System.out.println("Unable to withdraw. Insufficient units in store.");
return false;
}
}

Categories