Fundamentals of Computer Programming (Java) Assignment In this assignment, you w
ID: 3738512 • Letter: F
Question
Fundamentals of Computer Programming (Java) Assignment
In this assignment, you will create an array “buffer” without filling it right away. That means that you have an array but most of the values are meaningless zeroes. At the beginning, all of the values are meaningless zeroes. Only as you begin putting values into the array do some of the elements become meaningful. The important idea to capture, is that the “size” or capacity of the array, and the number of values in the array (or “count”), are two separate numbers. Start a project, as always. Create your main class with the same name as the project. Then create a new class called IntegerBuffer. The IntegerBuffer class will have two private variables: an array of ints (private int buffer[];), and a count of the number of values that have been added, called numberOfValues. The class should have two constructors. The constructor that takes no arguments should create an array of 100 ints (using a constant called BUFFER_SZ) for initializing the array: buffer = new int[BUFFER_SZ];. The other constructor takes an integer argument and uses that to create the initial buffer array of that size. Both constructors should set the numberOfValues to 0.
The IntegerBuffer class should have the following methods, add (which scans an integer as input and puts its value in the array OR adds a random integer to the array using the Math.random()), The user is asked to choose between the two options. So either the user has to enter a number or a random number is added to the array.
getNumberOfValues (which is a getter to return the current numberOfValues), and printToConsole (which works like the printLiteral method in assignment 5, except that it only prints as many values as have been added).
The key to making the add() method work is that when the array is empty (nothing yet added), it puts the first value in location 0, which would be buffer[0] = value;. After getting its first value, the numberOfValues should be “incremented” so it becomes 1. Then next time add() is called to add a value, that value should be put in location 1: buffer[1] = value; and the numberOfValues is incremented as before, and becomes 2. We cannot write the actual array index in the assignment, because each time we add a new value, that index must be one bigger than the array index we used before. But notice that the array index that we want to use is the same value as the numberOfValues before the new value is added. In other words, when the numberOfValues is 0, the next value goes in location 0. When the numberOfValues is 1, the next value goes in location 1. When the numberOfValues is 18, the next value will go in location 18. So the add() method should use the numberOfValues variable as the array index for the new value, and then increment it after the new value has been added to the buffer array: buffer[count] = value;
numberOfValues++;
Call the add method within a while loop which asks “Do you want to add numbers to the array?”
The loop should end when the value given is “No” (or some other clever mechanism if you want to make this more generally useful). When you run your program, you should be able to use it to enter a series of numbers using add. Your code should then show how many numbers you entered and print (using the print method) only the numbers that you entered. As a last step, make the IntegerBuffer more robust and safe from buffer overflow. Make the add method compare the count to the buffer length, first. If count is greater than or equal to the length, no value is added. The return type should be changed to Boolean (bool) so that it returns true when the value is added, and false when it is not, due to having reached the end of the buffer.
Explanation / Answer
There are two classes one is main class which is JavaProject.java and other one is IntegerBuffer.java.Here i am putting both classes ina a package named arraybuffer.
code for JavaProject.java
package arraybuffer;
import java.util.Scanner;
import arraybuffer.IntegerBuffer;
public class JavaProject
{
public static void main(String args[])
{
IntegerBuffer buf=new IntegerBuffer();
String loopcont="";
boolean opt=true;
Scanner sc=new Scanner(System.in);
System.out.println("Welcome to the System");
do
{
buf.add();
System.out.println("Do you want to continue:Y/N");
loopcont=sc.next();
if ( loopcont.equals("y") || loopcont.equals("Y") || loopcont.equals("Yes") || loopcont.equals("yes"))
{
opt=true;
}
else
{
opt=false;
System.exit(0);
}
}while(opt);
}
}
code for IntegerBuffer.java
package arraybuffer;
import java.util.Scanner;
public class IntegerBuffer
{
final int BUFFER_SIZE=2;
private int buffer[];
private int numberOfValues;
public IntegerBuffer()
{
numberOfValues=0;
buffer=new int[BUFFER_SIZE];
}
public IntegerBuffer(int size)
{
numberOfValues=0;
buffer=new int[size];
}
public void add()
{
int choice=0;
int val=0;
Scanner input=new Scanner(System.in);
if(numberOfValues<BUFFER_SIZE)
{
System.out.println("Please select an Option for adding a number to list");
System.out.println("1. Enter Input");
System.out.println("2. Select by system Randmoly");
choice=input.nextInt();
switch (choice)
{
case 1: System.out.println("Enter a Number");
val=input.nextInt();
buffer[numberOfValues]=val;
numberOfValues++;
break;
case 2: val=(int)Math.random();
buffer[numberOfValues]=val;
numberOfValues++;
break;
default:System.out.println("Wrong Input");
}
System.out.println("Total Values you entered in Array are");
System.out.println(getNumberOfValues());
System.out.println("Äll Values you entered in Array are");
printToConsole();
}
else
{
System.out.println("Buffer is full");
System.exit(0);
}
}
public int getNumberOfValues()
{
return numberOfValues;
}
public void printToConsole()
{
for(int loopval=0; loopval<numberOfValues; loopval++)
{
System.out.println(buffer[loopval]+" ");
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.