HOMEWORK / LAB WEEK 11_ We will continue this in class next week but give it a try and upload your attempt on Blackboard. YOU MUST HAVE THIS DONE ALREADY SO WE CAN MOVE ON. 1. Create a class named...

This is for Computer Programming Fundamentals 1. It is the first class in my Computer Science course so it is not suppose to be to advance. We use eclipse to write the code. I also added the lecture notes if needed


HOMEWORK / LAB WEEK 11_ We will continue this in class next week but give it a try and upload your attempt on Blackboard. YOU MUST HAVE THIS DONE ALREADY SO WE CAN MOVE ON. 1. Create a class named BankAccount_Lastname 2. //declare instance variables balance (double) interestRate (double) account number (int) //FIRST WE CHECKED THE DEFAULT CONSTRUCTOR; by instantiating the class 3. Create a class named TestBankAccount_Lastname. This class will contain the main method. 4. Instantiate three BankAccount objects. 5.For one of the bank accounts Display the value of each instance variable. Back to BankAccount class: 6. //CREATE CUSTOMIZED CONSTRUCTORS a. A constructor that takes as parameter a specific account number and creates an account with initial balance of $100 and 13% interest rate. b. A constructor that creates an account with a specified balance amount, interest rate and account number. (account number, rate and amount are given as parameters). c. A default constructor that takes as parameter a specific account number. Back to TestBankAccount class 7. Reconsider step1. Have the first BankAccount created with the first mentioned constructor Have the 2nd BankAccount created with the second constructor. Have the 3rd BankAccount created with the third (default) constructor. Back to BankAccount class 8. //(INSTANCE) METHODS Write a method named statement that displays (prints) all the instance variables. Back to TestBankAccount class 8a. Call the statement method on each of the created accounts. Observe the values of the instance variables for each account. Back to BankAccount class 9. Write a method called deposit that takes as parameter an amount and deposits it into your account. 10. Declare a static variable that will keep track of the number of created bank account. Update the variable in the constructor. Back to TestBankAccount 10a. Display the number of created account by printing the value of numberAccounts variable. 1 | P a g e 1115 - Week 11 Object and Classes Constructors Accessing Objects via Reference Variables Differences between Variables of Primitive Types and Reference Types Static vs Instance (methods and variables) Readings: Lecture Notes Textbook: Ch. 8 PPT presentation Static Methods Static Method is a method that is not associated with a specific instance of the class. static indicates that the method is a standalone method. That is why main method is a static method. Math.random() is a static method. You don’t have to create an instance of the Math class in order to use it. Instance Methods nextInt() is a method of the Scanner class and is NOT a static method. You need to create an instance of Scanner in order to use it. Invoking Methods: Static method vs. Instance method Instance method: CAN invoke an instance method CAN invoke a static method CAN access an instance data fields CAN access a static data field Static method: CANNOT (directly) invoke an instance method CAN invoke a static method CANNOT (directly) access an instance data field CAN access a static data field 2 | P a g e The “this” keyword When referring to instance variables you can use the this prefix. It is not required, however it allows you to see which variables are instance variables and which variables are local variables associated with a particular method. The “this” keyword is like a special variable that points to the memory address where the current instance resides in the heap. The this word forces Java to look at the instance variable (rather than a local variable) Methods created for instantiated classes do NOT have the static modifier. NOTE: you cannot access a static variable using this public class Example1 { public static void main(String[] args) { int x = 10; example2(x); } public static void example2(int a){ System.out.println(a); System.out.println(this.a); Error: cannot use this in a static method. } } Example: What will be the output of this code? public class Test { public int x = 10; //instance variable public void example(){ int x = 5; //local variable System.out.println("this.x is " + this.x); System.outprintln("x is " + x); } } public class TestTest { public static void main(String[] args) { Test a = new Test(); a.example(); } } 3 | P a g e Constructors A constructor is a special type of method. o Must have the same name as the class itself. o Does NOT have a return value. Not even void. o Are invoked using the new operator when an object is created. o Are used to construct a new instance of an object. They initialize the object. Example public class CircleWithConstructor { // data fields for a circle double radius; // constructors // user wants to set up a circle with a radius public CircleWithConstructor(double r) { // assign the supplied argument to our radius this.radius = r; } // user didn't supply a radius - just use 1.0 public CircleWithConstructor(){ // assign a default radius this.radius = 1.0; } } An object can have multiple constructors with the same name; the JVM will determine which one to call based on the provided arguments. If you do not supply any constructors, the compiler will automatically make a default constructor: It takes no parameters It initializes all instance variables Default Values of Instance Variables Integers: 0 Doubles: 0.0 Booleans: false Chars: \u0000 Arrays and other reference types: null Same as saying “this reference variable points to nothing”. It does not contain a memory address to anything on the heap, is not associated with any object. Check: DefaultValues.java TestDefaultValues.java 4 | P a g e What is wrong in the following class? public class Exercise { public static void main(String[] args) { Exercise t = new Exercise(5); } } LAB PART 1 Bank Account INSTANCE VARIABLES Create a class called BankAccount that does the following: Stores a balance (double) Stores an interestRate (double) Stores an account number (int) CONSTRUCTORS  create a new bank account that takes a specified account number and sets its variables to specific default values ($0 and 1%)  creates a new bank account with specified data values  create a (basic) default constructor A constructor that creates an account with $0 and 1% interest rate and a specified account number. The constructor will take as argument the account number. A constructor that creates an account with a specified amount, interest rate and account number. Write a TestBankAccount class Instantiate accounts using different constructors. METHODS A method called deposit that deposits an amount of money into your account. A method called withdraw that takes the amount to be withdrew, and withdraws the amount from your account. Returns true if successful, false if not. A method called statement that prints the account number, current balance and interest rate. 5 | P a g e static variables belong to a class. To create a static variable you need to use the static modifier. A static variable is shared by all objects of the class. There are also named class variables. For example: static int NUMBER_OF_LEGS = 4; static double totalAmount = 5000.00; static int numberOfObjects = 0; Q: Can static variables be changed? Yes class Cat { static int numberOfCats = 0; static int NUMBER_OF_LEGS = 4; String gender; String breed; String color; boolean neutered; public Cat (…) { //constructor numberOfCats++; } pet1 = new Cat(…); //numberOfCats is now 1 pet2 = new Cat(…); //numberOfCats is now 2 Lab: create a static variable numberOfAccounts that will be updated with each account created.
Apr 19, 2021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here