java sound amplifier method - java

I have an assignment for a class that has to do with implementing arraylists to work with sounds in java. I have tried to write the method but keep getting this error:
java.lang.ArrayIndexOutOfBoundsException:-254 (in java.util.ArrayList)
I am new to programming so I may have missed something quite simple. Anyway, here is my current method:
public void amplify (double amt) {
for(Integer i : myData){
myData.set(i, (int)(myData.get(i) * amt));
}
}
Thank you. Any help would be appreciated.

Try to use this you just create a local variable j and when you want to set the ArrayList use it like a index like below:
public void amplify (double amt) {
int j =0;
for(Integer i : myData){
myData.set(j, (int)(myData.get(i) * amt));
j++;
}
}

Related

Create a data structure with the features of an array in the constructor

I want to create a special list data structure that works like an array, in that it's like a list with values x[0], x[1], ... Any advice would be much appreciated.
I know all of my code isn't perfect I just want to figure out how to fix the one problem I've outlined below. This is some of the code I have:
public class SpecialList {
int[] specialList;
int lengthList;
public SpecialList(int x[]) {
this.lengthList = x.length;
this.specialList = new int[lengthList];
this.specialList = x;
for (int i=0; i<lengthList; i++) {
this.specialList[i] = x[i];
}
}
public SpecialList(SpecialList w) {
this.specialList = w.specialList;
}
public SpecialList doSomething(SpecialList y) {
int len = y.lengthList;
//The line below is an example to show the error I get
System.out.println(y[0]);
//Do some other stuff to the list y
return y;
}
//I test the code with this
public static void main(String[] args) {
SpecialList y = new SpecialList(new int[] {14, 17, 30});
SpecialList z = x.doSomething(y);
}
However I get the error 'array required, but SpecialList found' when I try to do stuff with y[i] like with System.out.println(y[0]); line of code.
'lengthList' works but getting the individual values of y[i] , the list doesn't. I cant work out whats wrong with my constructor for it to not work how I want it to.
You can't redefine what [n] means based on what object it's applied to; that's an array-specific notation. So y[0] where y is a SpecialList instance just won't work. If it could work, List (or at least ArrayList or other implementions where direct addressing is cheap) would probably have that feature.
Although you can do that in some other languages, you can't in Java. It's just not a feature Java offers. (Which is a good thing or a bad thing depending on your point of view...) Instead, you'll have to provide get and set methods or similar, just like List does.

Can't Find Symbol in my method instance?

This a very quick and i feel obvious mistake but i keep getting the CANNOT FIND SYMBOL
symbol : method print(int,int)
this would lead me to believe that i'm not giving the method the right data type parameters, however..
public class Test
{
public static void main(String[] args)
{
TestSrv srvObj = new TestSrv();
srvObj.print(0, 0);
srvObj.print(1, 1);
srvObj.print(2, 10);
}
}
and this method, what it's meant to do aside, i keep getting errors from the above code for all 3 calls to the print method? I am passing it integers on all 3 occasions?
public class TestSrv
{
public void print(int num, int count)
{
for (int i = 0; i <= count; ++i)
{
System.out.print(num + ". " + "*");
}
}
}
Your code should compile. Make sure that you declare both classes in the same package or that you import TestSrv in Test.java.
You almost certainly didn't compile TestSrv after making changes. Using an IDE such as Eclipse or IDEA will take care of much of that detail for you.
while renaming my method and class and such so that it wasn't what i originally named it(as to not confuse anyone) i actually fixed the problem that i had.. that is why this compiled for everyone xD i feel stupid! thanks again

Find and print the product of all the entries in array

Okay I have tried to write a simple Java code in BlueJ, that finds and prints the product of all the entries in data such as if data is {1,2,3,4} then the result will be 24.
And my code is below:
public class Product {
public static int[] product(int[] a) {
int [] s = new int[a.length];
for (int i =0; i< a.length; i++)
s[i] = a[i]*a[i];
return s; //the definition of your method...
}
public static void main(String[] args) {
//calling the method to seek if compiles
int[] results = Product.product(new int[] { 1,2,3,4 });
//printing the results
System.out.println(java.util.Arrays.toString(results));
}
}
The above code is giving me the square of each number, which is not what I want to have, somehow I have modify the code that the result will be 24 but I couldn't figure it out, anyone knows how to do it?
First of all, if you are first writing Java it is important to know that variable, function and class names are quite important. Please note that having Product.product() is not a good idea, since the function name is almost the same as the class name. Anyway, regarding your code. Your code is indeed returning the square of your input, what you would want is the following:
public class Product {
public static int getProduct(int[] input) {
int total = 1;
for (int v : input) {
total *= v;
}
return total;
}
}
This will return an integer value with the product of your input array. This also uses a for-each loop instead of a regular for-loop for readability. Also you don't need the index in this case. Good luck with it!
First, your product method needs to return an int rather than an int [].
You need to maintain the product as a variable. You can set it to 1 initially, and then multiply it by each element of the a array in turn; then you just return this value.

Understanding ArrayLists and how they are used for a TUI

Beforehand: This is a homework assignment so I need answers with explanations.
I am a first year student and my teacher is extremely vague while teaching so
I need some help understanding how to use the ArrayList in his instructions.
**I just need help understanding how this all works. I'm writing the code but I don't know how any of it works.
The Textual User Interface is supposed to be written in another class that I haven't done yet.**
My instructions were:
GradeManager Class 
Start by developing a class to describe a GradeManager. Because this exercise is far more about writing an interactive application, we'll keep this class SIMPLE. Our GradeManager will just keep track of an ArrayList of scores and define the following methods:
A single constructor that accepts no parameters, but sets up the ArrayList so that it is 
capable of holding some scores 
A method called addScore that will accept a score and add it to the ArrayList 
A method called getAverage that will return the average of all scores in the ArrayList
GradeTUI Class 
This class will keep track of a GradeManager object as an instance variable.  You will need to write a 
constructor that accepts the GradeManager object that it will be using.  
 
This class will also contain a method named run.  This method will give the user the ability to either (1) 
enter a score to be stored (in the GradeManager object) or (2) display the average of all scores entered 
so far.  Allow the user to continue this program by choosing one of these options until they are finished. 
 
Driver Class 
Once you are satisfied that these classes are defined and working properly, develop a driver class 
containing a main method that will create an instance of GradeManager, pass this object to the 
GradeTUI constructor, and then use the GradeTUI object to call run. 
This is what I wrote:
public class GradeManager {
private ArrayList<Integer> gradeList;
public GradeManager() {
ArrayList<Integer> gradeList = new ArrayList<Integer>();
this.gradeList = gradeList;
}
public void addScore(int score) {
this.gradeList.add(score);
}
public double getAverage(ArrayList<Integer> gradeList) {
Integer aScore = 0;
if (!gradeList.isEmpty()) {
for (Integer grade : gradeList) {
aScore += grade;
}
return aScore.doubleValue() / gradeList.size();
}
return aScore;
}
}
First: I think the getAverage Method should use the internal ArrayList instead of a parameter.
Otherwise you don't have any read access to that lists contents.
You can use your GradeManager in the following way:
GradeManager manager = new GradeManager();
while(condition) {
int score = readScoreFromInput();
manager.addScore(score);
}
double averageScore = manager.getAverage();
print(averageScore);
So you get the average of all the scores you have just input.
The concrete implementation of the loop (and the exit condition) is up to you - it could be a predefined number of iterations or a special input like -1 for example.

BubbleSort using integer Array

I have been trying to implement Bubble Sort using simple integer array in java. However there seems to be some problem. Now i know that using ArrayList would be the best option and I would do that too. But why isnt it getting sorted with simple integer array.Here is the code
package sort;
public class BubbleSort {
int array[]={1,5,3,32,54,6,87,5,1};
int temp=0;
public void enter(){
for(int i=0;i<array.length;i++){
for(int j=0;j<(array.length-i);j++){
if(array[j]>=array[j+1]){
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}
}
}
public void show(){
for(int i:array){
System.out.println(i);
}
}
public static void main(String str[]){
new BubbleSort().Enter();
new BubbleSort().Show();
}
}
Its producing the same array as entered. Nothing is getting changed. The difference between a simple array and an ArrayList or Vector ,is just that they offer dynamic time expansion of array size.Is there anything more to it? I mean does simple array creates a different instance every time it is manipulated, just like Strings? It does seem to do so here.
The problem is that you're not assigning a name to the instantiation of your BubbleSort class.
new BubbleSort().Enter();
new BubbleSort().Show();
Your code creates a new BubbleSort class, and then sorts it. And then it creates another new (and completely separate) BubbleSort class, and displays that one instead - and it hasn't been sorted.
You want to give a name to your variable, so you can sort it and then display it, like this:
BubbleSort myBubbleSort = new BubbleSort();
myBubbleSort.Enter();
myBubbleSort.Show();
As a side note (and as pointed out in SiB's answer), you may also want to check out the Java Naming Conventions. Following these conventions makes your code more legible to other Java programmers, and includes things like using lowerCamelCase for method names, and UpperCamelCase for class names.
Because you are sorting one instance and showing another.
new BubbleSort().Enter();
new BubbleSort().Show();
Use
BubbleSort bubbleSort = new BubbleSort();
bubbleSort.Enter();
bubbleSort.Show();
Also you should rename Enter() to enter() and Show() to show() to say the least.
Because you are creating two different BubbleSort Objects, sorting the first one and displaying a different one.
It should have been....
public static void main(String str[]){
BubbleSort sort = new BubbleSort();
sort.Enter();
sort.Show():
}
And the correct BubbleSort code is:
int temp = 0;
for (int i = 0; i < array.length; i++) {
for (int j = 1; j < (array.length - i); j++) {
if (array[j - 1] > array[j]) {
temp = array[j - 1];
array[j - 1] = array[j];
array[j] = temp;
}
}
}
I hope it helps someone else looking for it.

Categories