Involving Array Of Objects And Referencing Methods Of Each
Nov 26, 2014
I'm working on a project that involves the following:
-Creating a superclass of bankaccounts
-Creating two subclasses, checkingaccount and savingaccount
-Each of the two subclasses has different methods (writeCheck for checking, for example)
-Both types are created in a main class bank and stored in the same array
So let's say a user goes through the menus and creates a few savingAccounts and a few checkingAccounts (stored in the accounts[] array). Then, to write a check from one account, the user can enter the account number (a string), and the method will use a for loop to cycle through the array until it hits an account number match. Then it checks that it's the correct account type and calls methods from the subclass.
Problem here is that some methods work and some don't. In the following example:
for (BankAccount account: accounts) {
if (account.getAccountType().equals("Checking")) {
do {
if (account.getAccountNumber().equals(accountNumber)) {
amount = Double.parseDouble(JOptionPane.showInputDialog(
[Code] .....
The getAccountNumber method works but writeCheck is throwing an error. I tried creating a method in the superclass and overriding it in the subclasses but with no success.
So I want the method getDisplayText of d to be returned as long as the for loop runs. When I do this I get a compile time error to enter a return statement. I am currently trying to fix it by returning an empty string, yet this doesn't seem the right way to go.
public static String displayMultiple(Displayable d, int count) { for(int i = 1; i <= count; i++) return d.getDisplayText(); return ""; }
1. Modify the following class so that the two instance variables are private, there is a constructor that accepts both the player's name and sport and there are get and a set methods for each instance variable:
public class Player { String name; String sport; }
2. You can pass an instance of this class to the JLabel constructor if you want to display text in the label.Select one: a. myLabel b. myText c. String d. JTextField e. JLabelText
how to start making this work?cause i am not familiar with the terms here and want to complete this program and I am new to java programming?
in my progrm there are three diff array of objects...namely garments..gadgets and home app...now one who buys from each of these sections will have to make a bill at last...when he choses to make the bill he will be shown the list of products he bought and their details (like price...brand...etc)...so i thought that while he orders each product(which is done in a previous method called purchase()...)....(each product is stored as an object in there diif arrays namely garments...gadgets ...appliances)....each of those object will be copied in a new array in a diif class...then that array print will give me the desired result...
is this approach correct...?and if its correct then how can i pull out a specific obj frm a stored array of object and then save it in a new array....?
I have created a program that will take in input of paper names and authors. The next step of the program is to make references from paper to paper, and be able to add as well as list those reference, and i am lost as to where to go.I currently hold my instances of my paper class (the different papers I have stored) in a hash map. With the title being the key.
Is there a method as to where I can link keys? Do i need to use a seperate data structure? Or maybe add a new attribute to the paper class? I have attached the program as a zip file, If you open the zip file then its the methods in the Research SupportApp class .
public class demo { Public class static void main(String[]args) { //Creating a variable that will be a reference to the object Peoples person_one;
[Code] ....
I have assembled this code below that has a void method which will creat a new object. Problem I encounter is that in
Create_object(person_one);
the person_one has an error saying not initialized. I'm jus trying to learn on my own ways here and practice so may know what's wrong with this? I know I can use a return object from methods but what about this approach?
I want to make a program where users are prompted to enter a username and a password and have these two values create a new instance of the Object User. But I'm not sure where to start.
import java.util.Scanner; public class Main { public static void main(String[] args) { createUser();
[Code] ....
how to take username + password and put it into an object.
I have two classes, MonsterGame (shown below) and Monster. Monster has a public accessor method to get it's private character variable, which represents the first character of the name of the Monster in question.
I'm just a bit confused as to why am I unable to cycle through the array of Monsters I have in the MonsterGame class and call
m.getCharacter(); (pretty much the last line of code) public class MonsterGame{
private static final char EMPTY_SQUARE = ' '; private char[][] gameBoard = new char[10][10]; private Monster[] monsters = new Monster[4]; private int maxXBoardSize = gameBoard.length -1; private int maxYBoardSize = gameBoard[0].length -1;
[Code] ....
I understand that there need to be instances of objects to call methods, but is that not the case here? the Monster objects are have already been created, no? Do I need to create an index for the array? is the for loop not enough?
I was doing a project in a usual Java Application, but now maybe I have to use some tools that are contained in javafx.scene.media.MediaPlayer so I wonder if can it all work? Will javafx methods and such works?
I have to use javafx.scene.media.MediaPlayer beacuse I have to create a very simple audio player (one jbutton and one jcombobox).
We're learning how to use Binary I/O commands...which equates to....
My issue is trying to relate the Fraction objects (which we are to create using a loop) with the read/write methods used in Binary I/O (input/output streams). I left a blank after the output.write(), so you can see where the issue exist.
Java Code:
import java.io.*; public class FractionTest { public static void main(String[] args) { int [] fraction = new int[3]; for(int i = 0; i <= fraction.length; i++){ Fraction numbers = new Fraction();
This program is basically complete. It compiles and runs. It is a college course assignment that I pretty much completed but for the last part in which I'm suppose to change the values of all fields and display the modified values using a toString method. Modifying the values of the fields is where I am stuck. I don't think I need to create a new text data file to do this. The instructor only asked that all the values of fields be changed and this was the last part of the assignment so I don't think it involves creating additional ObjectOutputStream and ObjectInputStream objects. I'm getting a NullPointerException error on line 161.Here is the code. I'm also including the input data file.
//create program so that objects of class can be serialized, implements interface Serialiable //create constructor with 4 parameters with accompanying get and set methods, Override toString method //create text file with 5 records, create Scanner object,ObjectOutputStream, and ObjectInputStream //create new ItemRecord object, change values of all fields in ItemRecord object using object's set methods //modify ItemRecord object using toString method
[hightlight =Java]import java.io.Serializable; public class ItemRecord implements Serializable
----jGRASP wedge2: exit code for process is 1. ----jGRASP: operation complete.
Here is the data file: A100 99.99 10 Canon PowerShot-135 A200 149.99 50 Panasonic-Lumix T55 A300 349.99 20 Nikon- D3200 DSRL A400 280.99 30 Sony- DSC-W800 A500 97.99 20 Samsung- WB35F
Here is the data file for the modified field values. B100 98.00 10 ABC1010 B200 97.00 15 DEF1020 B300 96.00 10 GHI1030 B400 95.00 05 JKL1040 B500 94.00 01 MNO1050
1. Prompt the user to input two positive integers: firstNum and secondNum (firstNum must be smaller than secondNum). 2. Output all the even numbers between firstNum and secondNum inclusive. 3. Output the sum of all the even numbers between firstNum and secondNum inclusive. 4. Output all the odd numbers between firstNum and secondNum inclusive. 5. Output the sum of all the odd numbers between firstNum and secondNum inclusive.
*Use while loop int firstNum, secondNum; Scanner keyboard = new Scanner(System.in); System.out.println("Enter an integer: "); firstNum = keyboard.nextInt();
[Code] ....
What to do with the while loop and how to find even and odd numbers.
Exercise the Coin class using a driver class in which 5 coins are flipped, as a group, 1024 times. Display the number of times exactly 4 tails are obtained. Display the number of times exactly 5 tails are obtained. Use an array of Coin.
how to put an object into an array?I know how to make a coin class with a Heads and Tails, but I dont know how to make 5 coins all at once.
public class School { public final int MIN_GRADE = 0; public final int MAX_GRADE = 100; private int [] grades; public School( int students ) { grades = new int[students];
[Code] ....
The output is :
The numbers in the array are: 1 2 3 4 6 7 8 9 10 13 14 15 The highest grade is: 11
For some reason it is not giving me the highest number in my array. Did I write the method wrong ? Is my array even passing through HighestGrade ?
I have created a text file that contains a small list of toy names and prices, like:
Barbie, 12.95 Lego, 15.99 Hot Wheels, 5.00 Power Rangers, 6.49
And what I would like is my application to read the contents of the file and store the toy names in a list component. And then I want to be able to select a toy name from the list and add it to a shopping cart that is a list component as well. I want to the application to have menu items and buttons to allow me to remove items from the shopping cart, clear the shopping cart of all selections, and check out.
When I check out, the application should calculate and display the subtotal of all the toy names in the shopping cart, the sales tax (which can just be 8 percent of the subtotal), and the total. create this simple application example I've just made up, and I'm going to add and use this example to create a bigger application myself.
I know the first line below creates an object but the second line creates an array of cars, I'm just not sure how it does that. I can see sportsCar is one object in the array but the others are written in a different way. It seems there should a couple names there instead. Please explain the syntax in this code that's highlighted. I don't know how to make sense of it and I've read through the book where I got it where it explained creating objects.
Auto sportsCar = new Auto("Ferrari", 0, 0.0); Auto [] cars = {new auto("BMW", 100, 15.0), sportsCar, new Auto()};
I have a list of objects in my bean (ex: List<Apple> applies = new ArrayList<Apple>() ).The object has several fields (ex: supplier, color, width, height, breadth, etc.)I want to show this list on the front end. However I also want to allow the user to edit the attributes of the apples.So from the front end I will have like a list of fields where a set of fields is related to a single object in the list.I would also like to add/delete apples.
What first came to my mind is to map every field in the object Apple to a List. For example if Apple has field suppliers and field color then in the bean I would create two Lists, List<String> supplier, and List<String> color.From the front end I would display the contents of these Lists rather then the List<Apple> apples.T
he name of the field would be the same for each set of attributes.On save (form submit) the Lists would be re-populated with the field values (changed or not) and then I would be my Apple objects from the bean before saving in database.However I am not sure if there is something in JSF that can achieve this in a simple way, working only with List<Apple> rather than adding additional Lists.
okay,I created a program that demonstrated an array holding a number of objects, each from different classes.the printout I got for the first program ended up being gibberish. when I made the program a second time, I got the correct result.I've been trying to pinpoint the anomaly but I can't seem to find it. So ill try breaking down what I did?
Goal:The purpose of the program was to have a single Class (we'll call the Class 'Animal') to pass the parameters of a previously made method. This method would, amongst other things, call on an array.Further, I would then make subclasses of the Class Animal, and then use them as well to pass the method parameters, thus demonstrating subclasses can be used in place of the super class.
3 of these classes were made purely to as a "place setter". the Classes called "Cat" and "Dog" respectively were created to be subclasses of the Class Animal (with no other code inside them aside from "extends Animal)the 3rd "place setter" called "ClasstoHoldObjects" was created to be the array class (ClasstoHoldObjects itself has no code in it aside from the class name)these are the ones we want.Animal class (which holds the coding meant to be used).Here is the Animal Class
public class Animal { private ClasstoHoldObjects[] thelist = new ClasstoHoldObjects[5]; int i = 0; public void add(ClasstoHoldObjects x){ if(i < thelist.length){
[code]....
the print out was this next, im putting THIS line in the array... june_6.Dog@a0dcd9 (and the same by the Cat object).I recognize june6 as the package name.
finally, I erased extra lines as well as comments and simplified class names to make the code more readable. If you want the original, or some class names are a bit off- I could give you the original with all its comments and longer named classes
I have a program I want to make (text based, no gui). There is the main class, an Employee class (sort of a template), a CrewMember class, and a Manager class.
I'll put the code for each class an explain the problem I have.
package polymorphism; import java.util.Random; public class Start { public static void main(String[] args) { Random rand = new Random(); Employee staff[] = new Employee[5]; for(int i = 0; i < staff.length; i++){
[Code] ....
Some of the code is a bit incomplete simply because I ran into the problem. As you can see I made an array in the Start class and it holds objects of Employee type, but create a new instance of either a crew member or a manager, sets their wages, hours, and bonus if applicable. I know if I create an array of a certain type, I can't call upon the subclass' method (Manager in this case) because it has a new method that I added. What I'm trying to do is pretty much call upon the getSalary() method in the Manager class/object, but of course I can't. What way would i be able to do that? I tried looking for some answers. I read about making the superclass abstract and implementing it into the subclasses. Would that be an option?
The following code is supposed to generate random integers and sort array1 and array2 using two different sorting methods. array1 is to be sorted with a "selection sort" method and array2 is to be sorted with the built-in Arrays.sort() method. However, array1 is the one that has a problem. It does not appear to output any values at all for array size of 4000 or more, such as array1[10000]. The assignment is to generate random integers, sort and benchmark the speeds at which array1 and array2 can generate and sort ints at array1[1000] array2[1000] array1[10000] array2[10000] array1[100000] array2[100000]
/** * * The following is a sorting and benchmarking program to sort * array1 and array2 with 1,000 , 10,000 and 100,000 array sizes. * array1 uses selection sort from section 7.4 of the book * and array2 uses the built in Arrays.sort() method.
[code]....
I cannot post the output because the amount of data seems to have crashed the two previous posts I made on this topic due to the size of the problem.
Alright so I'm trying to write a code that implements a for loop and if statements that displays any number from 100-200 if the number is divisible by either 5 or 6 in rows of ten numbers each row. If it is not divisible by that number then it should go back to the beginning of the loop until it reaches 200. My main problem is that it doesn't display anything. I don't get any errors or anything but every time I run the program it just displays nothing. Sample output is at the bottom of the code.
public class Exercise5_11 { public static void main(String[] args) { int count = 0; int i = 100; //for (the numbers from 100 to 200) for (i = 100; i>100 && i<200; i++){
I am learning Java on my own and I am taking on very small project by myself for fun, and I'm just stuck on this small part of the project. So I have created a text file that contains a small list of toy names and prices, like:
Barbie, 12.95 Lego, 15.99 Hot Wheels, 5.00 Power Rangers, 6.49
And what I would like is my application to read the contents of the file and store the toy names in a list component. And then I want to be able to select a toy name from the list and add it to a shopping cart that is a list component as well. I want to the application to have menu items and buttons to allow me to remove items from the shopping cart, clear the shopping cart of all selections, and check out. When I check out, the application should calculate and display the subtotal of all the toy names in the shopping cart, the sales tax (which can just be 8 percent of the subtotal), and the total.
I'm not sure if my understanding of PriorityQueues is correct, so I'm trying to check if my reasoning is valid. I'm supposed to compare the Big-O for arrays and linked lists for the following instructions:
Insert 100 objects having the priorities 1, 2, 3, ... , 99, 100 Big-O for Array: __________ Big-O for Linked List: ___________
Insert 100 objects having the priorities 100, 99, 98, ... , 2, 1 Big-O for Array: __________ Big-O for Linked List(Assume no tail reference): ___________
If my understanding is correct, priority queues take in items randomly with no particular order, but they are removed according to the priority of each element. If what I've said is true, wouldn't that mean that inserting any number of objects would be O(1) for both linked lists and arrays? If the PriorityQueue has no particular order, then wouldnt each add() simply insert something to the next array index/linked list node?
How do you declare methods for a class within the class whilst objects of the class are declared else where?
Say for instance, I have a main class Wall, and another class called Clock, and because they are both GUI based, I want to put a Clock on the Wall, so I have declared an instance object of Clock in the Wall class (Wall extends JFrame, and Clock extends JPanel).
I now want to have methods such as setClock, resetClock in the Clock class, but im having trouble in being able to refer to the Clock object thats been declared in the Wall class.
Is this possible? Or am I trying to do something thats not possible? Or maybe I've missed something really obvious?
An array has objects with String firstName, String lastName, int ID, char examType('M' or 'F'), and int score. Every object has both Midterm('M') and Final('F'). The array has to be sorted by the first Midterm- 'M' and immediately followed by the Final ('F') of the same person (object). Im having troubles with coming up with a proper algorithm with gettin the Final- 'F' after Midterm for the same person.
Java Code: public static Exam[] collateExams(Exam[] exams) { Exam [] r = new Exam[10]; r = exams; int[] position = new int[10]; int index = 0; for(int i = 0; i < exams.length; i++)