How To Give Values To Array Objects Without Using Loop
Apr 3, 2014
The main method should creates a Student object with name as "Bill" and marks as {88,92,76,81,83} and print it.
Java Code:
class TestStudent {
public static void main(String args[]) {
Student1 st = new Student1();
st.name = "bill";
Student st1[] = new Student[6];
//This gives error
st1[].marks = {1, 5, 8, 9, 7, 6}; mh_sh_highlight_all('java');
View Replies
ADVERTISEMENT
May 6, 2014
Create an application that allows a user to enter values for an array of seven Salesperson objects. Offer the user the choice of displaying the objects in order by either ID number or sales value.
import java.util.*;
public class SalesPersonSort
{
public static void main(String args[]) {
int[] id = new int[7];
int[] salesValue = new int[7];
final int END = 999;
[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
Apr 16, 2014
Started learning about Array's I'm doing an exercise where you create a for loop that randomly assigns values to each element within the array, but where is my code going wrong?
import java.util.Scanner;
public class ArrayExamples{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
double exampleArray[] = new double[5];
System.out.print("Enter a Number: ");
int num1 = input.nextInt();
[Code] .....
View Replies
View Related
Feb 18, 2014
int[][] array1 = {{1, 2, 3}, {}, {1, 2,3, 4, 5}};
it initializes 3 one dimentional array:
1 - {1, 2, 3}
2 - {}
3 - {1, 2,3, 4, 5}
but it only declares two dimensional arrays.
View Replies
View Related
Oct 28, 2014
Whether I pass primitives or objects the original value does not change. Is this expected result?
Java Code:
public class Class {
public static void main(String args[]) {
int x=3;
doItPrim(x);
System.out.println(x);//3
Integer i=new Integer(3);
[Code] ......
View Replies
View Related
Feb 12, 2014
Is it possible to use a loop to create objects? I'm trying to put together a program (just as a way of learning) to run a horse race.
What I'd like to is have the user enter the number of horses they want in the race, and then create the objects as the user enters the various attributes of the horse (at least the horse's name), and then once all the instance variables have been set, create the first horse object and then move on to the second one, etc, until all the horse objects have been created.
I know I can't use something like a for loop to do it, so how would the code be writte
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
Nov 3, 2014
Everything is written up and looks good and i get no compile errors but every student object other than the default constructor has null and zero values when printed.
import java.text.DecimalFormat;
import java.lang.Math;
public class Student
{
//instantiate variables
[Code] .....
View Replies
View Related
Nov 22, 2014
I want to loop through each of my objects and call the method for each one. ( polymorphic array )
Error: Constructor Dog in class Dog cannot applied for gives Types
Required String,String
Found: no arguments
Reason actual and formal argument lists differ in length
Java Code:
public class animal {
private String m_type="";
private String m_name="";
public static void main (String[] args) {
[Code] ....
View Replies
View Related
May 4, 2014
I've written a die program, and i want the user to type any key in order to continue, how do i achieve this ?
I have already created a Die class, i just want to implement the main method now.
PHP Code:
package test;
/* This program demonstrates the use of a user-defined class
public class RollingDice {
// Creates two Die objects and rolls them display the values.
public static void main (String[] args) {
Die die1 = new Die();
System.out.println("Roll the die ")
}
} mh_sh_highlight_all('php');
What should i add after System.out.println, So that the user types (any key, or types "Enter" for example, for the die to roll). i dont want the die to roll by itself i Want the user to interact with the program in order for it to roll.
View Replies
View Related
Oct 13, 2014
This assignment requires me to show areas of each shape by using loop. I can do it with abstract and interface , but in this case. I don't know how to use method getArea() to loop for each object
import java.util.ArrayList;
public class TestShape {
ArrayList<Shape> list = new ArrayList<Shape>();
Circle c;
Rectangle r;
Square s;
public TestShape() {
[Code] .....
View Replies
View Related
Oct 4, 2014
I know the normal way of naming objects is
Pipe pipe1 = new Pipe
but I want the objects to be made inside a loop and named after how many times the loop have been gone through so I tried
Pipe pipe(numberOfTimes) = new Pipe
where numberOfTimes was a variable counting the loops. This is not working.I need the naming to be pipe1, pipe2, pipe3 etc depending on how many times the loop have been pased
Scanner keyboard = new Scanner (System.in);
String morePipes = ("yes");
int dimRor;
int numberOfPipes = -1;
[code]....
View Replies
View Related
Jul 8, 2014
I have a problem with my application. It supposed to store 4 different Room objects but when I entered one only it stores tat object variables into all my Array elements. I just need it to store any number of objects as long as it is less than 4.
Java Code:
import java.util.Arrays;
import java.util.Scanner;
import javax.swing.JOptionPane;
class TestRoom {
public static void main(String [] args)
{
String[] roomsInHouse = new String[4];
[Code] .....
View Replies
View Related
Apr 21, 2014
So I'm trying to write a program that takes in 20 numbers and computes their average and I'm supposed to use a while loop to read the values and to make sure that all the numbers are positive. My while loop is set up and does all the math correctly but I have completely forgotten how to taken 20 numbers from the user all at once and then run them through the while loop.
HERE IS MY CODE SO FAR:
public static void main(String[] args) {
int num = 100;
int count = 0;
int total = 0;
while (num >= 0 && count < 20){
if (num %2 == 0){
total += num;
count++;
}
}
double avg = total/20;
System.out.println("The Result Is: " + avg);
}
View Replies
View Related
Dec 10, 2014
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
[Code] .....
This is the error message:
----jGRASP exec: java ItemRecordReport
Item Cost Quantity Description
Number
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
Exception in thread "main" java.lang.NullPointerException
at ItemRecordReport.main(ItemRecordReport.java:161)
----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
View Replies
View Related
Mar 7, 2014
My intention is to see the real reference values (memory locations) of 2 StringBuilder objects as below:
StringBuilder sb2 = new StringBuilder("123");
StringBuilder sb3 = sb2;
System.out.println(sb2);
System.out.println(sb3);
I was expecting a printout like
@S234853
@S984724
But all I got was 123 printed twice. How can i print out the reference values instead of what is inside the StringBuilder object ?
View Replies
View Related
Sep 25, 2014
The program is supposed to print all even and odd numbers between 50 and 100 using ONE while or do while loop. The evens work, but the program terminates after printing 51 for the odds, which doesn't satisfy the second value sentinel value for the while loop.
public class EvenOdd {
public static void main(String args[]) {
int x = 50;
int y = 50;
int i = 0;
int j = 0;
System.out.print("Even numbers between 50 and 100: ");
[Code] ....
View Replies
View Related
Sep 29, 2014
Write java program using table that stores celsius and farenheit values that are equal to one another using a loop. use C 0-20 and convert to farenheit.
I have to use doubles for Celsius and Fahrenheit and in the formula. I get a runtime error with the following displayed:
I will display a table of temperatures in their Celsius and Farenheit equivalents.
celsiusfarenheit
import java.util.*;
import java.lang.*;
import java.io.*;
class TemperatureConversion {
public static void main (String[] args) throws java.lang.Exception {
double celsius;// Temperature in degrees Celsius minimum
double farenheit;// Temperature in degrees Fahrenheit
[Code] ....
View Replies
View Related
Jan 5, 2015
I have a program where i want to indicate the user when i have completed a task which i am running inside a for loop. Below is my code.
for(Map.Entry<Double,SimplestCopyInstance> entry : myList.entrySet()){
double key = entry.getKey();
SimplestCopyInstance scp = entry.getValue();
Copy cp = new Copy();
cp.putLocations(scp.getSrc(), scp.getDes());
scp.setStatus(cp.baseCopy()+"");
[Code] ....
I have used netbeans to build my app. So there creating jTable is out of my control as that part was inside auto-generated code. I have just used the jTable.setValue().
My problem is, above method is inside a button click event. Updated values not displaying until the loops ends.
View Replies
View Related
Nov 2, 2014
I had to write a foor loop to count the values of a stack of quarters from 1.00 to 3.00 and I had to print the values, that I understood and got it working so I taught the next assignment was going to be easier but I am having a hard time with it. For this one I have to write a for loop to print all the positive integer factors of 144 and I am supposed to print of factor per line but I tried doing that but it doesn't work it just prints out 144.
This is my code. The quarter assignment is also in there because it is part of a lab so just ignore that part since it is working correctly.
public class ForLoopPractice
{
public static void main(String [] args)
{
// Write a for loop to count out the values of a stack of quarters from $1.00 to $3.00
// Print the value of each iteration. Print this all on one line, rounded to the nearest cent.
// To print rounded, use printf, with a placeholder of %.2f
// (%f is the floating-point placeholder; the .2 indicates the number of decimal places)
/* YOUR CODE HERE */
for (double q = 1.00; q <= 3.00; q += .25)
[Code] ....
View Replies
View Related
Jun 15, 2014
Write a method that returns a new array by eliminating the duplicate values in the array using the following method header: public static int[] eliminateDuplicates(int[] list). The thing is that I found the working solution that is written below, but how it works. How to eliminateDuplicates method done with flag and flag2.
Here is the code:
Java Code:
import java.util.Scanner;
public class Exercise06_15 {
public static void main(String[] args) {
java.util.Scanner input = new java.util.Scanner(System.in);
int[] numbers = new int[10];
System.out.println("Enter ten numbers: ");
[code]....
View Replies
View Related
Feb 16, 2015
filling out a Random array: An Array of Specific Length Filled with Random Numbers This time what I need to do is take the elements from this Random array and assign them to a new Byte array:
for(int i = 0; i < limit-10; i++) {
Random dice = new Random();
int randomIndex = dice.nextInt(array.length);
if (array[randomIndex] < 128) {
System.out.print(array[randomIndex] + " ");
} else if (array[randomIndex] >= 128) {
System.out.print(array[i] + " ");
}
}
byte[] noteValues = new byte[]
{ 64, 69, 72, 71, 64, 71, 74, 72, 76, 68, 76 }; //This is the byte array filled manually!
I've tried amending the manual input to fit in with the Random array, as follows:
byte[] noteValues = new byte[]
{ array[randomIndex] };
In this case, however, the Byte array can't interpret the int values. Also, if the Byte array is outside the 'for' loop, array[randomIndex] cannot be resolved.
View Replies
View Related
Mar 28, 2014
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.
View Replies
View Related
Apr 10, 2014
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()};
View Replies
View Related
Apr 24, 2014
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.
View Replies
View Related