Automatically Increasing Array Size?
Feb 5, 2014
I'm working on an assignment that says the following.
" The array size is fixed in Listing 10.6. Improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array to it.Implement the dropStudent method.Add a new method named clear() that removes all students from the course.
Write a test program that creates a course, adds three students, removes one, and displays the students in the course."
10.6 Listing
public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
}
public Course(String courseName) {
this.courseName= courseName;
[Code]...
My Test Code based off of book
public static void main(String[] args) {
Course course1= new Course("Business");
course1.addStudent("Jay");
course1.addStudent("Silent Bob");
course1.addStudent("Dante");
course1.dropStudent("Jay");
[Code]....
My adjusted 10.6
public class Course {
private String courseName;
private String[] students = new String[100];
private int numberOfStudents;
}
public Course(String courseName) {
this.courseName= courseName;
[Code]...
The problem I'm having is, for the first part of the question where I need to automatically increase the array size. I'm really not great at this stuff. I have tried breaking it down, but can't "get it", I guess.
I assume, it'd be a loop that checks to see if the student array is full and if so, do the increaseArray() part, by maybe multiplying the student array and then assigning it. I just don't know how to do it haha.
My *best* attempt at the loop so far has been
if (students == students.length){
int bigArray = 2*students.length;
String increaseArray()= new String[students];
System.arraycopy(students, 0, increaseArray, 0, students.length);
students= increaseArray;
but,yeah... Doesn't seem to be right.
View Replies
ADVERTISEMENT
Dec 8, 2014
The array size is fixed improve it to automatically increase the array size by creating a new larger array and copying the contents of the current array into it .I am using a course class and here is the code for the class
public class Course {
private String courseName;
private int numberOfStudents;
private String[] students = new String[100];
public Course(String courseName)
[Code] ....
As you can see I have the array set to size 100, how do i make so it increments each time the user adds a student.
View Replies
View Related
Jul 11, 2014
I am trying to make a code that copies the users String to a char array. However, I am in a predicament: since I would not know the exact size of the users String I am left with the options of either making my array large by default, filled in with, lets say 25, empty spaces per index OR starting out with a default size of 1, with an empty space, and then some how increase the size from there.
At this moment I am leaning on the first option, creating a large default array and then deleting the unused values. However, this brings me to my actual question: if I set the non used indexes to null, if that wont give me an error, would that change the size of my array?
Ex:
//lets say i finally copied all of the values and this is the result
char[] word = {'b', 'o', 'b', ' ', ' '};
for(int i = word.length(); i > 0; i--){
if(word[i] == ' ')//delete the value so the size decreases
word[i] = null;//if possible
}
View Replies
View Related
Nov 21, 2013
I am using a 64 bit Win 7 Pc with 64-bit JVM and we get the error: Java heap space. So we want to increase the Java heap size but not for one application but for every application or in general.
We tried with the java -xmx command but it didn't work...
We tried setting the system variable JAVA_OPTS but again it didn't work...
View Replies
View Related
Mar 24, 2015
I am taking the Class Algorithms and Datastructures and got an assignment for Lab that really throws me off. The goal is to create an Array out of a given CSV file, implement several Methods that get the size of array, etc.
I am still stuck in the first part where the CSV has to be imported into the Array. My problem is that I need a mechanism that figures out the needed size for the Array, creates the array, and only then transfers the data from the CSV.
The list consists of the following wifi related values:
MAC-Adress, SSID, Timestamp, Signalstrength.
These are on the list, separated by comma. The Columns are each of these, and the rows are the four types of values making up the information on a certain wifi network.
The catch is, we are not allowed to use any of the following:
java.util.ArrayList
java.util.Arrays
and any class out of java.util.Collection.
So far I used the BufferedReader to read in the file and tried to implement the array, but I get an arrayindexoutofboundsexception.
Below is my Code (Its still an active construction zone):
public class WhatsThere {
public WhatsThere(String wifiscan) throws IOException {
}
public static void main(String[] args) throws IOException {
// WhatsThere Liste = new WhatsThere(String wifiscan);
String[][] arrayListe = new String[0][0];
[Code] ....
View Replies
View Related
Feb 15, 2015
I have the textfile which has lines:
A B 2 midterm 100
C D 1 final 90
C D 1 midterm 90
A B 2 final 80
**NO ARRAYLIST IS ALLOWED!** And the textfile is passed into the method. How to get the size for the array non-randomly inside the method from the passed Scanner file?? What if you have lots of numbers of lines, so how could that be done?
I have doubts about this line Exam[] object = new Exam[12];
Java Code:
public static Exam[] readAllExams(Scanner s) throws ArrayIndexOutOfBoundsException
{
String firstName = "";
String lastName = "";
[Code].....
View Replies
View Related
Jan 14, 2014
I come to the point: I just started to learn java through various manuals and in one of them I came across a declaration of an array that I do not understand:
int[][] multiArr = new int[2][];
the manual says that you can allocate the multidimensional array multiArr by defining size in only the first square bracket but I can't undestand how you can use this array. Seems to be no way to store data with it!
View Replies
View Related
Oct 28, 2014
I am having trouble with an assignment. I need the user to input the size of the array and print when asked. In my program, it prints 100 numbers instead of the user input number, such as 15.
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;
public class Lab9 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int [] values = new int [100];
[Code] .....
View Replies
View Related
Oct 17, 2014
protected void randomise() {
int[] copy = new int[];
// used to indicate if elements have been used
boolean[] used = new boolean[array().length];
Arrays.fill(used,false);
for (int index = 0; index < array().length {
[Code] .....
View Replies
View Related
Jan 9, 2014
Is this possible in Java?
public void doSomething(int[4] year){
}
What I am trying to do is to get the person who is using the method to send a year in the format 1998 and so on.
What would be the best way to accomplish this?
View Replies
View Related
Jan 29, 2015
I'm just putting together a little 'horse racing' program while I'm learning Java, and I have a class called Race that creates an array list of thorougbred horses called field, and asks the user to enter then names of the each horse in the field, with a maximum of 14 horses. The problem occurs with the current code that I have:
import java.util.*;
public class Race {
RaceHelper helper = new RaceHelper();
ArrayList<thoroughbred> field = new ArrayList<thoroughbred>();
public void setField() { //enter the horses in the race and determine the size of the field
[code]....
the statement of the index position and current size was for me, so I could see what was going on.What I don't understand is the while loop. not that the class doesn't compile and run (you can see that it does), it's the output. Why does the <= sign allow one more entry and increase the size of the field to 15?
Less than or equal to 14 should give a maximum field size of 14, right, With the starting object at index position at zero and going up to 13, for a total size of 14 thoroughbred objects if I just use while (field.size()<14) or a for loop, then the output is fine; it allows a max of 14 entries and prints the results. I thought it had something to do with the size being zero based, but that doesn't seem to matter -- unless it does matter and I'm missing it. why the comparison I'm using produces this output? a field of 14 horses shouldn't matter whether it's zero or 1 based, as long as the size of the field is 14, so why the extra entry with this while condition?
View Replies
View Related
Dec 3, 2014
I'm working on a program to create a blackjack game using objects (one for card, deck. and hand). Withing my hand object I am trying to add cards to the hand but it is only adding the last card i try to add and giving null values for the the ones before.
class BlackJackHand {
private BlackJackCard [] hand;
public void addToHand(BlackJackCard c) {
if (hand == null) {
BlackJackCard [] tempHand = new BlackJackCard[1];
tempHand[0] = c;
hand = tempHand;
[Code] ....
What I want this section to do is add cards to the current hand. I was intending for it the hand to be null at first and the if(hand == null) piece to add the card the first time and then the else piece would be used when the hand already has at leas one card. I want the else section to create a temporary array that is one larger than my current hand, copy the cards from the old hand to the new hand, and then add a new card to the last space before rewriting the old hand as what the temporary hand is.
The code I am using to test if the addToHand() is working is
class BlackJackTest
{
public static void main (String[]args) {
BlackJackCard c1= new BlackJackCard(1,0);
BlackJackCard c2= new BlackJackCard(1,4);
BlackJackCard c3= new BlackJackCard(1,5);
BlackJackHand h1 = new BlackJackHand();
[Code] .....
BlackJackCard has the parameters (int suit, int value)
This should print:
ace of clubs
4 of clubs
5 of clubs
but instead prints:
null
null
5 of clubs
View Replies
View Related
Aug 2, 2014
Write a program to create an integer array of size 20. Then, the program should generate and insert random integers between 1 and 5, inclusive into the array. Next, the program should print the array as output.
A tremor is defined as a point of movement to and fro. To simulate it, the program will generate a random number between 0 and 19, which represents the location in the array (i.e. index number). Then, the 3 numbers to the left and right of this location should be reset to the value 0. If there isn't 3 numbers to the left and right you may assume a lesser number depending on the boundaries of the array.
Then, the final array should be printed as output. There is no user input for this program.Your program must include, at least, the following methods:
-insertNumbers, which will take as input one integer array and store the random numbers in it.
-createTremor, which will generate the random number as the location and return it.
A sample run of the program is shown below:
Sample output #1:
Array:1 2 2 3 1 5 4 2 3 4 4 2 1 1 3 2 1 4 3 2 1
Random position: 5
Final Array:1 2 0 0 0 5 0 0 0 4 4 2 1 1 3 2 1 4 3 2 1
View Replies
View Related
Aug 18, 2014
Write a program to initialize and display variable size array.
View Replies
View Related
Feb 15, 2015
We are writing our own classes and methods. My instructor has provide the code
/*-------------------------------------------------------------------------
import java.util.*;
public class Assignment5 {
public static void main (String[] args) {
[Code].....
I am having trouble with the question counter. I need the counter to increase when c, d or e are entered. I think I need to set up a if or while loop but I'm not sure how to setup the variables. This is what I have for the counter so far.
[public int getNumberOfQuestions(){
numQuestions = 0;
numQuestions ++;
return numQuestions;
}
View Replies
View Related
Sep 3, 2014
Consider the sequence of digits from 1 through N (N<=9) in increasing order: 1 2 3 4 N
Insert either a +(for addition) or a - (for subtraction) between each of the digits so that the resultant sum is zero. Print all possible combinations that sum to zero.
Example: Enter a number: 7
1+2-3+4-5-6+7=0
1+2-3-4+5+6-7=0
1-2+3+4-5+6-7=0
1-2-3-4-5+6+7=0
View Replies
View Related
Aug 29, 2014
In class we were give the algorithm for a non-decreasing algorithm here:
This is pseudo code given for the non-decreasing.
for j = 2 to A.length
key = A[j]
i = j - 1
while i > 0 and A[i] > key
A[i+1] = A[i]
i = i -1
A[i = 1] = key
//I was asked to make it non-increasing, so this is what I have.
for j = A.length - 1 to 1
key = A[j]
i = j - 1
while i > 0 and A[i] < key
A[i+1] = A[i]
i = i + 1
A[i-1] = key
Is there anything wrong with this algorithm? Will it give me the non-increasing sort?
View Replies
View Related
Jan 28, 2015
Okay so I need to be able to read a file and then be able to find the longest sequence of increasing numbers.
So if the file was this, (this is Ass1Q2_test4.txt)
97 47 56 36 60 31 57 54 12 55
35 57 41 13 82 80 71 93 31 62
89 36 98 75 91 46 95 53 37 99
25 45 26 17 15 82 80 73 96 17
75 22 63 96 96 36 64 31 99 86
12 80 42 74 54 14 93 17 14 55
14 15 20 71 34 50 22 60 32 41
90 69 44 52 54 73 20 12 55 52
39 33 25 31 76 45 44 84 90 52
94 35 55 24 41 63 87 93 79 24
the output should be,
(5,0) with cost 12
(6,0) with cost 14
(6,1) with cost 15
(6,2) with cost 20
(7,2) with cost 44
(7,3) with cost 52
(7,4) with cost 54
(6,3) with cost 71
(5,3) with cost 74
(4,3) with cost 96
Greatest path is of length 10.
Now, the code that I have works, kind of. Instead of recurring several times at each point, it only recurs once.
So say I'm looking at (1,1). With (1,1) being 57. The area around it looks like this.
97 47 56
35 57 41
89 36 98
Now when I look at it, there are several paths it can take. It can go 57, 97 or 57, 89 or 57, 98. However, I'm pretty sure that it just uses the first one that corresponds with the first if statement that is valid. So I start checking north of the value, then northeast, then east, then southeast, which at southeast is where I find my first greater than value. After it finds it's first valid number, it then continues from that number, instead of checking if there are other longer paths stemming from the original value.
In conjunction with that, you can see that the printout just returns all paths from each value. Which isn't what I want. I need a way to store the longest current path, then check each path after to see if it's longer. If it is, it's replaced, if not, it stays the same.
I've also attached Ass1Q2_test4.txt
import java.util.*;
import java.io.*;
public class MaxIncreasingSub {
[Code].....
View Replies
View Related
Nov 18, 2014
Write a program that asks the user for an integer andthen prints out all its factors in increasing order. Use a class FactorGenerator with a constructor FactorGenerator(int numberToFactor) and methods nextFactor and hasMoreFactors. Supply a class FactorTester whose main methods reads a user input, constructs a FactorGenerator object and prints the factors.
Here is what I have so far for my main class and tester class
public class FactorGenerator
{
int factor;
int number;
int x;
FactorGenerator(int numberToFactor)
{
number = numberToFactor;
}
[Code]....
View Replies
View Related
Jan 27, 2015
I am trying to print a loop inside an array of a greater size than the loop itself. Like for example I have an array of size 7 but it has only 3 elements.
int[] array=new int[7]
array[0]=2;
array[1]=3;
array[2]=4;
now what I want to do is print these three numbers in a loop so that my array[3]=2;array[4]=3;array[5]=4 ...... till the last one. Also the array could be any size and not just 7.
View Replies
View Related
Feb 9, 2015
I am trying to create an array list without using the built in arrayList class that java has. I need to start the array being empty with a length of 5. I then add objects to the array. I want to check if the array is full and if not add a value to the next empty space. Once the array is full I then need to create a second array that starts empty and with the length of the first array + 1.
The contents of the first array are then copied into the new array without using the built in copy classes java has and then the new array ends up containing 5 elements and one null space the first time round. The first array is then overwritten with the new array containing the 5 elements and the null space. The process then starts again adding values until the array is full which will be 1 more value then recreating the second array empty and the length of the first array + 1. This is my attempt at creating this but I have run into multiple problems where I get only the last value I added and the rest of the values are null:
public class MyArrayList {
public Object arrayList[];
public MyArrayList(Object[] arrayList) {
this.arrayList = arrayList;
[code]...
View Replies
View Related
Oct 12, 2014
1. Is this the JVM ?
2. If so, it uses Java version 7. Who uses Java 8 ?
3. If we install Netbeans, does the JVM gets installed automatically?
View Replies
View Related
Feb 19, 2015
I'm writing a JavaFX desktop app, and would like to make it automatically run after installation, but I'm not sure where to look to make this happen (or even if it's possible?). I have successfully created a .dmg installer using E(fx)clipse & Ant. Are there options I'm overlooking which I can use in the build.xml? or am I looking in the wrong place entirely?
View Replies
View Related
Aug 31, 2014
I am trying to get random shapes to generate automatically. I was able to get just three shapes to generate but nothing random. When I added the random code now only the frame shows up and no shapes. I also keep getting an error with frame.setVisible(true). It says identifier expected. Here is what I have so far:
Main program:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class ShapeGen {
public static void main(String [] args) {
//Create window and set title
draw panel = new Draw();
[Code] ....
This is the draw program to generate random shapes:
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class draw extends JPanel {
public draw(Color backColor) {
setBackground(backColor);
[Code] .....
View Replies
View Related
Jun 7, 2014
The program shall assign a new employee ID to the employee and display it on the screen.
The employee ID shall be generated by adding 1 to the largest employee ID already in the employee.txt data file.
You can see I've tried variations of identification++ but have not been successful. I've also tried to get the last or the highest identification in the arrayList but have not done that right.
public static void addEmployee() {
String firstName = Validator.getString(
sc, "Enter First Name: ");
String lastName = Validator.getString(
sc, "Enter Last Name: ");
int identification = 0;
[Code] ....
I also can display all of the employees their identifications and their punches but I cannot narrow it down to searching one employee and displaying information for just the one.
-If the selected value is ‘I’, prompt the user to enter the employee’s ID number.
-If ‘I’ is selected the display shall show the employee’s name and ID, list out each day worked in chronological order, the number of hours worked that day and a total number of hours worked in the two week period.
The report shall prompt the user to re-enter an employee ID of it does not exist in the employee file.
private static void displayReports() {
System.out.println("Report");
Scanner sc = new Scanner(System.in);
String action = " ";
while (!action.equalsIgnoreCase("d"))
[Code] ....
View Replies
View Related
Apr 23, 2014
I need a list of all the dates in 2014 in "YYYY-MM-DD" format (because I need to load them into a database), and I do not want to type them all myself. So is there a way to actually get it generated automatically?
View Replies
View Related