Declare Array And Populate It With Log Formula
Feb 10, 2014
Still trying to get a handle on arrays! So, I declare an array to be a 46x1 and I am trying to populate it with a Log formula that I am using but I keep getting an ArrayIndexOutofBoundsException.
Code :
private double[][] LNValues = new double[46][1];
//Calculating y=LN(E-k) and Initializing the Array
for(int x=0; x<LNValues.length; x++)
{
double i = Math.log(eValues[x][1] - kValue);
if(i > 0)
[Code] ....
View Replies
ADVERTISEMENT
Nov 15, 2014
We were given a class lab that asks us to write a program that create a multidimensional array ( 5 x 5 ), populates the array using nested loops with letter from A until Y, and displays the array to the screen. and the result should look like this:
A B C D E
F G H I J
K L M N O
P Q R S T
U V W X Y
How to write this program.. I have tried all my best but the results are not coming like this..
View Replies
View Related
Jun 5, 2014
I have a jtable which i want to put an array into... and Really don't know how.. I dont want to use array list mainly because it looks way to complicated so there must be some other way using defaultablemodel but i dont know how..
So here's my code: just a snippet which includes the testing of the jtable
String[] columns = {"col1", "col2", "col3", "col4", "col5", "col6"};
//declare 2d array
String[] [] data = {{"1", "2", "3", "4", "5", "6"}};
jt = new JTable (data, columns);
jt.setPreferredScrollableViewportSize (new Dimension (50, 80));
//set initial selectibility to false
jt.setFocusable (false);
jt.setRowSelectionAllowed (false);
JScrollPane tableContainer = new JScrollPane (jt);
frame.getContentPane ().add (tableContainer);
View Replies
View Related
Apr 16, 2015
1.How to declare array in java?
2.How to push elements in it in key-value value pair in java?
For example-
This is the way i do it in javascript..
I want to do like this in java..
var my_array;
for(var i = 0; i<5; i++)
{
my_array.push({x: i, y:0});
}
where,push is builtin method in javascript.
View Replies
View Related
Oct 28, 2014
I want to declare a 2 dimensional array in java which has 3 column and unlimited number of rows, i want to give a special name to each column . The type of first column is string second one is int and the last one is string
Column1_name
Column2_name
Column3_name
String value
Int value
String value
.
.
.
.
.
.
View Replies
View Related
Nov 3, 2014
The main Method
-Create a main method declares and creates an integer array called nums that can hold 15 integers.
-Use a for loop to fill that array with multiples of 3: 0, 3, 6, 9, etc.
-Then use similar for loop to print each value in the array on one line, with each value separated by a single space.
-Compile and run the program to see the result:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
As you write other methods, you'll also modify the main method to make calls to them. The printArray MethodWrite a method called printArray that accepts an integer array as a parameter. This method does not return a value, and must be declared as static so that the main method can call it. Instead of printing the array in the main method, move that loop into this method. Call the printArray method from the main method. Compile and run the program to verify it prints the sam result as before.Add a println statement so that after printing the array values on one line, it then moves to the following line.Finally, modify the loop in the printArray method so that, instead of using a traditional for loop, it instead uses a for-each loop. Compile and run the program again.
Part III: More Array Methods
The linearSearch Method In lecture we looked at a method that performed a binary search on a sorted array. A much simpler (though much less efficient) search is a linear search, that simply starts at the front of the array and looks at each element in turn until it finds it or reaches the end.Create a method called linearSearch that accepts an integer array and a single int value as parameters. The goal of the method is to find the second parameter (the target) in the array. The method should return a single int representing the index of the target value. This method should not print any output itself. In this method, use a traditional for loop to scan through the elements in the array. As soon as you find the target value, return the index of that value.
If you scan through the entire array without finding the target value, return a -1.Modify the main method to call the linearSearch method and print the results. Call it twice, searching for the value 18 (which it should find) and the value 10 (which it should not). Including the previous activity, the output of the main method should now look similar to this:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sumArray Method
The sumArray method should take an integer array as a parameter and return a single integer representing the sum of all values in that array.Use a for-each loop to access each value in the array and compute a running sum. After the loop, return the total.Call the method from the main method, producing the following augmented output:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315
The addValue Method...The addValue method should accept an integer array and a single int as parameters. The purpose of the method is to add the second parameter to EACH value in the array. The addValue method does not return a value, but the elements inside the array will be modified. Call the addValue method from the main method, adding 100 to each element in the array. Then call the printArray method again to see the modified array values:0 3 6 9 12 15 18 21 24 27 30 33 36 39 42
The index of 18 is 6
The index of 10 is -1
The sum of this array is 315 100 103 106 109 112 115 118 121 124 127 130 133 136 139 142
Test a Different Array..Finally, duplicate the content of the main method to perform similar tests on another array. Instead of filling it with multiples of 3, fill it with multiples of 4. And instead of using an array size of 15, use an array size of 20.Modify the values search for to include one that is in the array and one that isn't.Rerun the main method and carefully check the results.If you haven't been doing it all along (which you should), make sure the appropriate class and method documentation is included.When you're satisfied that all methods are working correctly, modify the main method to delete the second array tests.
View Replies
View Related
Apr 14, 2015
I have this working manually, but need creating this from a DB Connection and populating the Array with the results. I just need populating "DestinationItem[]" from the SQL below
DestinationBean.java
// Manual Array works but I need this to be populated from DB using the below Query and DB Connection info.
private DestinationItem[] destinationResults = new DestinationItem[]{
new DestinationItem("58285", "Dodge Grand Caravan"),
new DestinationItem("57605", "Dodge SX 2.0"),
new DestinationItem("58265", "Chrysler 300 Touring")
[Code] .....
View Replies
View Related
Nov 18, 2014
why I need to populate an array with leading zeros. It sounds like it wants me to populate the entire array for one number (string). When I use next(), it separates the numbers so why do I need to go the leading zeros route?
This assignment will give you practice with external input files and arrays. You are going to write a program that adds together large integers. The built-in type int has a maximum value of 2,147,483,647. Anything larger will cause what is known as overflow. Java also has a type called long that has a larger range, but even values of type long can be at most 9,223,372,036,854,775,807.The approach you are to implement is to store each integer in an array of digits, with one digit per array element. We will be using arrays of length 50, so we will be able to store integers up to 50 digits long. We have to be careful in how we store these digits. Consider, for example, storing the numbers 38423 and 27. If we store these at the front of the array with the leading digit of each number in index 0 of the array, then when we go to add these numbers together, we're likely to add them like this:
38423
27
To simulate this right-shifting of values, we will store each value as a sequence of exactly 50 digits, but we'll allow the number to have leading 0's. For example, the problem above is converted into:
0000000000000000000038423
0000000000000000000000027
Now the columns line up properly and we have plenty of space at the front in case we have even longer numbers to add to these.The data for your program will be stored in a file called sum.txt. Each line of the input file will have a different addition problem for you to solve. Each line will have one or more integers to be added together. Take a look at the input file at the end of this write-up and the output you are supposed to produce. Notice that you produce a line of output for each input line showing the addition problem you are solving and its answer. Your output should also indicate at the end how many lines of input were processed. You must exactly reproduce this output.
You should use the techniques described in chapter 6 to open a file, to read it line by line, and to process the contents of each line. In reading these numbers, you wont be able to read them as ints or longs because many of them are too large to be stored in an int or long. So youll have to read them as String values using calls on the method next(). Your first task, then, will be to convert a String of digits into an array of 50 digits. As described above, youll want to shift the number to the right and include leading 0s in front.
The String method charAt and the method Character.getNumericValue will be useful for solving this part of the problem.You are to add up each line of numbers, which means that youll have to write some code that allows you to add together two of these numbers or to add one of them to another. This is something you learned in Elementary School to add starting from the right, keeping track of whether there is a digit to carry from one column to the next. Your challenge here is to take a process that you are familiar with and to write code that performs the corresponding task.
Your program also must write out these numbers. In doing so, it should not print any leading 0s. Even though it is convenient to store the number internally with leading 0s, a person reading your output would rather see these numbers without any leading 0s.You can assume that the input file has numbers that have 50 or fewer digits and that the answer is always 50 digits or fewer. Notice, however, that you have to deal with the possibility that an individual number might be 0 or the answer might be 0. There will be no negative integers in the input file.You should solve this problem using arrays that are exactly 50 digits long. Certain bugs can be solved by stretching the array to something like 51 digits, but it shouldnt be necessary to do that and you would lose style points if your arrays require more than 50 digits.The choice of 50 for the number of digits is arbitrary (a magic number), so you should introduce a class constant that you use throughout that would make it easy to modify your code to operate with a different number of digits.
Consider the input file as an example of the kind of problems your program must solve. We might use a more complex input file for actual grading. The Java class libraries include classes called BigInteger and BigDecimal that use a strategy similar to what we are asking you to implement in this program. You are not allowed to solve this problem using BigInteger or BigDecimal. You must solve it using arrays of digits.Your program should be stored in a file called Sum.java.
Input file sum.txt
82384
204 435
22 31 12
999 483
28350 28345 39823 95689 234856 3482 55328 934803
7849323789 22398496 8940 32489 859320
729348690234239 542890432323 534322343298
3948692348692348693486235 5834938349234856234863423
999999999999999999999999 432432 58903 34
82934 49802390432 8554389 4789432789 0 48372934287
0
0 0 0
7482343 0 4879023 0 8943242
3333333333 4723 3333333333 6642 3333333333
Output that should be produced
82384 = 82384
204 + 435 = 639
22 + 31 + 12 = 65
999 + 483 = 1482
28350 + 28345 + 39823 + 95689 + 234856 + 3482 + 55328 + 934803 = 1420676
7849323789 + 22398496 + 8940 + 32489 + 859320 = 7872623034
729348690234239 + 542890432323 + 534322343298 = 730425903009860
[code]....
View Replies
View Related
May 26, 2014
I have a simple txt file, each line simply containing 1 word.I would like each work to represent an index of the array..im having some difficulty populating an array from either a txt file or a scanner.i seem to be able to fill the scanner so to speak with the contents of the text file but not the array. I don't know how to syntax it
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Arrays;
public class ReadFile
public static void main(String[] args) {
[code]....
View Replies
View Related
Feb 21, 2015
The experiments will slowly converge towards one big experiment: a simple game. I have just a little interest in games (perhaps I should have more), but making one - even a simple one - should be self rewarding.
However, now to the point.
* The experiment creates an array of rectangle objects.
* The rectangles are painted inside a Frame object at random x,y coords generated by a random number generator
* The rectangles are stationary.
* The rectangles are each assigned their own random colour.
* The array of rectangle objects is created inside the constructor of the class.
The actual code contains various other variables and methods which would distract from a quick analysis, so below is code which has the same logical structure which also fails (instead of array of rectangles, I have used arrays of integers).
import java.util.Random;
/**
* Experiment 14 - see if it works simply - (with integer arrays)
*/
public class TestingArrays {
// instance variables
int N = 10; // the size of the array - 10 elements
int[] a;
[Code]...
View Replies
View Related
Oct 26, 2014
Write a program that creates an array that can hold 9 double values that represent baseball batting averages for a starting baseball lineup. Use a for loop to populate array with random double values in the range of 0.00 to 0.500. Recall that "double" values are what Java calls "real" numbers. Use a second for loop to print the values in the array with one number per line. Finally, use a third for loop to traverse the array and find and print the maximum batting average in the array. Note: you will need to use String.format to control the precision of a double number when you print it- Here is my code so far:
public class P2F {
public static void main (String[] args) {
double [] player= new double [9];
//player[0]= Math.random();
for (int index=0; index < player.length; index++) {
[Code] ....
When I open the terminal window I get different variations of this [D@4545c5]. I would like to know all the things I am doing wrong.
View Replies
View Related
Feb 5, 2015
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class array
{
public static void main(String[] args)
[Code] ...
Is there a way to write this, where, alpha is one array.
Write a program that declares an array "alpha" of 50 elements of type "double". Initialize the array so that the first 25 elements are equal to the square of the index variable and the last 25 elements are equal to three times the index variable. Output the array so that 10 elements per line are printed.
If I have an array of 50 integers, can I break that to read in lines of 10?
View Replies
View Related
Apr 14, 2015
I have a quick polymorphism question. I have a parent class and a sub class that extends the parent class. I then declare an array of parent class but instantiate an index to the sub class using polymorphism. Do I have to have all the same methods in the child class that I do in the parent class? Here is an example of what I mean.
public class ParentClass
{
public ParentClass(....){ }
public String doSomething(){ }
}
public class ChildClass extends ParentClass
{
public ChildClass(....)
[Code] ....
Is polymorphism similar to interfaces where the child class needs all the same methods?
View Replies
View Related
Mar 30, 2014
What is accurate formula area and perimeter for pyramid and hexagon? I got stuck of my assignment.
View Replies
View Related
Mar 17, 2015
I am working on this new project where we are using the great distance formula but every time I run my ending result is NaN. I was researching, and people say its because you divide by 0. I think I have my formula correct.
Java Code:
public class testingFormula {
public static void main(String[] args) {
double lat = 34.01858902;
double lon = -118.2817688;
double lat2 =33.94530869;
double lon2 = -118.3994904;
[Code] ....
View Replies
View Related
Jan 28, 2014
It's suppose to generate a table with the month, and the new amount of the CD. Right now, the program generates a continuous table for months, but it doesn't update the value of the CD.
import java.util.*;
public class Excercise04_31 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner input = new Scanner(System.in);
[Code] ....
View Replies
View Related
Jan 20, 2015
I'm trying to convert a Formula to run in a java program.
This is the formula:
(you can ignore the modifier part)
This is my attempt. However it prints out 0.0
import java.util.Scanner;
public class DamageCalc{
public static void main(String[] args){
Scanner s = new Scanner(System.in);
[Code].....
View Replies
View Related
Sep 1, 2014
i am trying to calculate the number of months it takes to pay off a set amount of credit card debt given the following information: principle amount, annual interest rate, and monthly payment. How to import the formula necessary to system.out.print the answer (number of months it takes to pay off debt) ???
View Replies
View Related
Oct 3, 2014
I have just started to learn programming in the book I am teaching myself from it said how would you write the following arithmetic expression with it being the quadratic formula but only the plus part of it in the plus or minus..
package javalearning;
import java.util.Scanner;
public class QuadraticFormula {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter value for B: ");
double B = input.nextDouble();
System.out.print("Enter value for A: ");
double A = input.nextDouble();
System.out.print("Enter value for C: ");
double C = input.nextDouble();
double negativeOfB = -B;
[code]....
View Replies
View Related
Sep 6, 2014
I'm supposed to take an input from the user and calculate the value of pi using this formula: 1/1 - 1/3 + 1/5 - 1/7 etc... so on alternating between minus and plus and denominator increasing by 2 every time. If the user inputs the number 3, then it should print (1/1 - 1/3 + 1/5 - 1/7 + 1/9) *4. The idea here is that the higher the input of the user is, the higher the equation will be close to pi.
This is what i have so far :
import java.util.Scanner;
public class Pi
{
public static void main(String[] args) {
double first = 1/1;
double second = 1/3;
Scanner input = new Scanner(System.in);
System.out.println("Enter a Number to calculate how far you want the formula to perform: ");
double count = input.nextDouble();
}
}
This is literally all i have, i know i need a while loop and an if loop.
View Replies
View Related
Mar 4, 2015
Write a program named QuadraticTable.java that takes three double arguments and two int arguments (in that order) from the command line. The three doubles will represent a, b, and c from the quadratic formula.
If the discriminant is non-negative, find and print the solutions to the quadratic equation. (The plus/minus sign in the quadratic formula indicates that you would separately perform both operations). Otherwise print "no real solutions".
Then using the last two int arguments as x_min and x_max, print a table of x and ax
2 + bx + c
values using the a, b, and c read in as command line arguments.
Sample output: (command line arguments were 1 0 2 -3 3)
1x^2 + 0x + 2 has no real solutions.
X 1x^2 + 0x + 2
-3 11
-2 6
-1 2
0 2
1 3
2 6
3 11
View Replies
View Related
Jun 19, 2014
I have this formula for creating quarter wave antennas, however for some reason I can not get the right answer for it. double parM takes the users information which is a frequency in Megahertz and changes to feet so we can construct our antenna. However, in the program it gives me 1.9 instead 7.7 which it is suppose to be.
double parM = Double.parseDouble(userInput.getText());
double getSize = (234 / parM) * 12; // 12 is in inches
View Replies
View Related
Sep 19, 2014
So Ive got to make a turtle on an image draw a semicircle that starts at the top of a circle and goes down and to the left, changing colors halfway through. I've got everything down, I can draw the turtles and make them do straight lines. My problem is more math related. I need to use the equation of a circle, give it points, and figure out how to write that code in java.
We use the equation for a circle: (x - a)^2 + (y - b)^2 = r^2
amanda.setName("amanda");
amanda.setShellColor(Color.BLUE);
amanda.setBodyColor(Color.RED);
amanda.setPenColor(Color.YELLOW);
amanda.setPenWidth(3);
amanda.forward();
amanda.turn(-90);
amanda.setPenColor(Color.BLUE);
amanda.forward(???);
the question marks are where I need to draw a line on an arc, like in the picture.
View Replies
View Related
Feb 24, 2014
Basically its a program where a user is prompted to enter the length of all three sides of a triangle and the program calculates the area by herons formula and can tell if the triangle is equilateral or Pythagorean. I am having trouble entering a formula to where all three enter sides cant possibly be a triangle. Here is my Program. Where the '?' is stated.
import java.util.Scanner;
public class Triangle {
public static void main(String[] args){
double a;
double b;
double c;
double s;
double x;
double area;
[Code] ....
View Replies
View Related
Mar 29, 2015
Is it possible to declare a counter with a value outside of a for loop?
I have a counter that will end prematurely in a while loop during various iterations and I want to pick it back up in a catch all for loop at the end
Let's say I have a while loop
while(something) {
total++
}
then after I have a for loop that I want to start at total but would rather do that then make a new counter variable.
for(total;total < 20;total++)
Is something like this possible or is this a horrible thing to want anyways?
View Replies
View Related
Feb 14, 2015
The problem is to figure out the number of cartons needed to box up the strawberries picked by the farmer and his wife. The farmer picks 8.4 lbs per hour and the wife pick 10.8 pounds per hour. They pick from 8 am until 4Pm (8 hours). You can put 20 pounds per box.I understand the word problem and how to declare and initialize variables. I'm just confused how to display the math into java to solve it.
View Replies
View Related