• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1056
  • Last Modified:

[noob] how do I use private class? java

This code doesn't work - help me make it work!



public class usebankaccount
{

      public static void main(String[] args)
      {
            
            bankaccount acct1 = new bankaccount();
            acct1.deposit(500);
            
      }
      
      
}




private class bankaccount
{
      double penny;
                  
      
      public bankaccount()
      {
            System.out.println("an account has been opened and it has 0 dollars.");
      }
      
      public bankaccount(double pennies)
      {
            double penny = pennies;
                        System.out.println("an account has been opened and it has" + penny + "dollars.");
      }
      
      public void withdraw(double subtract)
      {
            penny = penny - subtract;
            System.out.println(subtract + "has been withdrew, the account still has" + penny + "dollars.");
            
      }
      
      public void deposit (double add )
      {
            penny = penny + add;
            System.out.println(add + "has been deposited, the account now has" + penny + "dollars.");
      
      }

      public void show()
      {
            System.out.println("This account has" + penny + "dollars.");
            
      }
      
      
}









0
Troudeloup
Asked:
Troudeloup
  • 2
1 Solution
 
CPColinCommented:
Change:

private class bankaccount

to:

class bankaccount
0
 
TroudeloupAuthor Commented:
http://instruct.langara.bc.ca/~hdarband/cpsc1181/lab/lab2/lab2.html

this homework is overdue and I think I did it wrong


would you still help me?

I don't want you to code it for me, but I want to get the concept -


does "immutable"  mean that I should have written it as


public class main
{
}

class rectangular
{
}


class polar
{
}


?


0
 
CPColinCommented:
"Immutable" means that you can't change anything in the class after you create it, so you can't give anything the opportunity to change its member data. This is usually done with private member data with public accessors ("get" methods) and no mutators ("set" methods). The class itself doesn't have to be private.
0
 
Jim CakalicSenior Developer/ArchitectCommented:
By example, String and BigDecimal are immutable classes in the JDK. Once they have been instantiated, they cannot be changed. If you want to add two BigDecimal objects, the result is a third BigDecimal object containing the sum:

    BigDecimal addend1 = new BigDecimal("123");
    BigDecimal addend2 = new BigDecimal("456");
    BigDecimal sum = addend1.add(addend2);

The result of calling the add method is a third BigDecimal object. The object represented by addend1 has not changed its value. It still contains an internal representation of the number 123 such that if you did this:

    BigDecimal sum2 = addend1.add(new BigDecimal("5");

the variable sum2 would now reference a fourth BigDecimal object with the value 128.

What you appear to be confusing is access level with immutability. Access level defines whether other classes can reference a field or invoke a method. All of your classes in this exercise could (and probably should) be public. The solution structure would look something like:

public class ComplexPolar {
    private BigDecimal real;
    private BigDecimal imaginary;

    public ComplexPolar(BigDecimal real, BigDecimal imaginary) {
        this.real = real;
        this.imaginary = imaginary;
    }

    public ComplexPolar add(ComplexPolar a) {
        return new ComplexPolar(this.real.add(a.real), this.imaginary.add(a.imaginary));
    }
    public ComplexPolar subtract(ComplexPolar s) {
        return new ComplexPolar(this.read.subtract(s.real), this.imaginary.subtract(s.imaginary));
    }
}

The ComplexRectangular class would be similar; it would contain fields to hold the real and imaginary parts of the coordinate and methods that would perform simple arithmetic operations on two ComplexRectangular numbers and return a third just as above with the ComplexPolar class.

BTW, I had to laugh at the statement (in the assignment): "do not forget that documentation is more important than code itself." A fundamental principle of agile methodologies is that working code is to be preferred over comprehensive documentation. It's not that documentation is bad. But having working sofware is better and more important to the business that is using the software. Without the code, all that documentation is just a waste of shelf space. Look, if you're a working software developer and your project manager tells you she needs a use case implemented by Friday, how do you think she will respond when you report your status on Friday and say, "Since documentation is more important than the code I spent all my time writing this great Javadoc. Here it is. 15 pages of documentation but not a single line of code."

Regards,
Jim
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now