How To Declare And Initialize Variables
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
ADVERTISEMENT
Oct 3, 2014
Write a program OutCircle.java that declares and initializes three floating-point variables (r, x, y): the first variable represents the radius r of a circle centered at (0,0) and the second and third variables are the coordinates (x, y) of a point in the plane.Your program should print true if the point is outside the circle and false otherwise. Hint: A point is outside the circle when its distance to the center is greater than the radius.
View Replies
View Related
Apr 1, 2014
I'm making a card game and have a class I used to assign all my card variables.
However, it's giving me multiple errors and I can't seem to be able to access the variables from my other classes.
Java Code:
package com.summoners.Screen;
class Cards {
public static String[] names;
name = new String[1000];
atk = new int[1000];
[Code] .....
View Replies
View Related
Oct 10, 2014
Java Code:
import java.util.Scanner;//Imports Scanner
public class operadoreslogicos//imports a public class {
public static void main (String [] args)//defines the state of a class {
Scanner input = new Scanner (System.in);//Inputs a scanner to read user
//keyboard
boolean cierto = true,// Declares variables as boolean and applies a value
[Code] .....
The word string is supposed to turn purple when I type it, but it just stays normal, so it doesn't declare hello and bye as variables.
View Replies
View Related
Mar 26, 2012
Any way of How to declare an ArrayList that can hold int variables?
View Replies
View Related
Sep 6, 2014
Where usually to instantiate and declare variables to be used in a loop. If you declare it outside to be used in the loop it will still be there when the loop is done, never to be used again and is just sitting there taking up memory. However, if you declare it inside the loop, you have the issue of it constantly creating space for said variable, but once it's out of scope it's gone. Is there any advantage performance wise to doing it either way?
View Replies
View Related
May 2, 2015
I'm working on a program where the user inputs song information (name, artist, filesize, duration) which is stored in a database. Once the user adds a song, the details should be stored in the first empty song slot (4 song slots total) and then taken back to the menu interface.
What I'm having trouble with is that I can add the first song fine (song1), but when I try to add another song, the information entered into song1 is gone. I've worked out that this is because every time I call upon the addNewSong() method, I'm creating four Song objects that are brand new with the line:
Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();
How to fix this problem is where I'm stuck because if I move these lines elsewhere, I get a 'cannot find symbol - variable song1' error.
Here is my code:
public class SongDatabase {
Scanner console = new Scanner(System.in);
public void addNewSong() {
Song song1 = new Song();
Song song2 = new Song();
Song song3 = new Song();
Song song4 = new Song();
if (song1.isEmpty()) {
[code]....
View Replies
View Related
Apr 29, 2014
What's up with this. Just trying to test my hands on java packages, and had this error(by java) after successful compilation:
Error occurred during initialization of VM
java.lang.Error: Properties init: Could not determine current working directory.
at java.lang.System.initProperties(Native Method)
at java.lang.System.initializeSystemClass(System.java :1119)
Main.java
package com.aceix.simplecalc;
import com.aceix.simplecalc.inputhandler.InputHandler;
import com.aceix.simplecalc.mathoperation.MathOperation;
public class Main {
public static void main(String[] args) {
[Code] ....
Any suitable way to accept input from console.
View Replies
View Related
Nov 13, 2014
Write a program to input a person''s age and salary, if the age is over 32, add $100 if otherwise subtract $500 from salary, finally output the age and new salary here is my code. I am getting a red line underneath the word 'salary' and i would like to know why and why i have to initialize it. and this is what i have tried.
</Scanner scan = new Scanner (System.in);/>
</int age, sum, difference, old_salary, new_salary, salary;/>
</System.out.println("Please enter the person age");/>
</age = scan.nextInt();/>
[code]....
View Replies
View Related
Aug 10, 2014
initialize the min and max to be the first numbers entered, need to improve the code, because if the number entered is < 0 there is an error. but i dont understand how i would initialize to the first number entered, my understanding is tha if the user enters a 5 , then i should initialize to value 5, but i cant anticipate what the user will enter. I could initializa to null, but that still is not the first number entered.
int min = 0;
int max = 0;
int option = JOptionPane.YES_OPTION;
while ( option == JOptionPane.YES_OPTION){
[code]....
View Replies
View Related
Sep 22, 2014
Trying to make me understand when to initialize a string 'null' and when not..??
View Replies
View Related
Feb 13, 2014
I tried this:
String[][]f = new String[1][1] {{"Harry"}{"Hairy"}};
I also tried this:
String[][]f = new String[1][1] {{"Harry"},{"Hairy"}};
but I get an error
View Replies
View Related
Aug 18, 2014
Write a program to initialize and display variable size array.
View Replies
View Related
May 20, 2014
After this problem is fixed, my math game will run. The answer variable is supposed to be equal to the cases in the second switch statement by random but I get errors whenever I try to initialize it this way. I have been writing this program for almost 5 days now and it is finally wrapping up. Why I can't map the answer variable to the second switch statement the way I want to?
package pkgnew;
import java.util.Scanner;
import java.util.Random;
public class New {
public static void main(String args[]) {
//Declare and construct variables
Random randomnum = new Random();
int fnum, snum, answer;
[Code] .....
View Replies
View Related
Sep 9, 2014
I'm trying to make an array of objects, which I then initialize using objects that I have already created. I have main class, a secondary "Other" class, and a third "Other2" class. In the first Other class, I create three objects of the Other2 class, an object array of the type Other2 , and I then try to add the three objects to the object array, which results in the errors:cannot find symbol: class objectArray, ] expected, identifier expected
here is my code:
Other class
public class Other
{
Other2 object1 = new Other2();
Other2 object2 = new Other2();
Other2 object3 = new Other2();
[code]....
View Replies
View Related
May 25, 2012
I am trying to use a custom listener to initialize database connection pool (C3P0) on start up and then destroy on context shut down. The reason for that is that I whenever context is shutdown I have a memory leak because initialized connection pool is not being destroyed.
I have a static class called C3P0Utils that deals with connection pool. In my listener in contextInitialized method I have tried at first to init the pool like this :
+public void contextInitialized(ServletContextEvent sce) {+
+try {+
C3P0Utils.newInstance().init();
+} catch (PropertyVetoException ex) {+
Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
+}+
+}+
I know that object is created. I checked it using jconsole. However it is not accessible with in the application. My second attempt was to regester the pool and then add it to the context and then when I need it read from there.
+public void contextInitialized(ServletContextEvent sce) {+
+try {+
ServletContext ctx=sce.getServletContext();
C3P0Utils.newInstance().init();
ctx.setAttribute("myDataSource", C3P0Utils.newInstance().getDataSource());
+} catch (PropertyVetoException ex) {+
Logger.getLogger(DatabaseInit.class.getName()).log(Level.SEVERE, null, ex);
+}+
+}+
However when I try to red from the context I get nothing. I try to read like this.
+(ComboPooledDataSource)ctx.getAttribute("myDataSource")+
When I print names of all of the attributes in the context I get these attributes.
Context Name
org.apache.tomcat.InstanceManager
org.apache.catalina.jsp_classpath
javax.servlet.context.tempdir
org.apache.catalina.resources
+org.apache.tomcat.JarScanner
+org.apache.jasper.compiler.TldLocationsCache
+org.springframework.web.context.WebApplicationContext.ROOT
Why I can not use anything initialized in the listener.
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
May 27, 2015
Write a program that declares and concatenates various strings. Declare strings for your first name, middle initial and last name, along with one for your address, city, state and zip. Make an additional string called firstLine, which will be a concatenation of first name, space, middle initial, period, space, and last name. Assign each of these strings a value, and then print the information in the following format:
First Line (First name Middle initial (period) Last name)
Address
City, State
Zip
For example:
John Q. Public
1234 Any Street
Cleveland, Ohio
44101
Now reassign the strings in the same program (Do not create a separately compiled program!) and repeat the printout for different information. When you are finished, your program will print out two groups as above.
The information should be placed into the seven separate strings, then a first line should be formed by concatenating first name, space, middle initial, period, space, and last name. The city and state line should be formed by printing city followed by a comma, a space, then the state (NOT by concatenation). This is so you can see different ways of making lines."
And this is what I have so far:
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
Aug 3, 2014
I mean i need to access to two txt file at the same time so i did this in method:
DataInputStream din= new DataInputStream(FileInputStream("vip.txt"));
DataInputStream din2= new DataInputStream(FileInputStream("corporate.txt"));
But there are errors when i compile and they pointing to these two lines.
View Replies
View Related
Apr 26, 2015
I'm trying to code a little text RPG. I've made a little "personality test", with 4 questions (answers a b c), where every letter stands for a type of personality. The analysis of the result is simple counting of the answers, if you have 3 answers a (4 questions), then a has won. If 2 on a, and 2 on b, a simple Random method shows weather result a or b.
Now... the test should give you a "partner" (imagine the pokemon game with 3 different starter pokemon). I have now 3 string variables, like 3 different partners. They are all declared somewhere outside, but i only need one later.
Like...the test is completed, you have your partner and how can i make now that the program is just showing me my partner? Like...when i type " System.out.println(partner); " (outside of the test loop! ) that i only get the one i got through the test?
I was trying to declare in every loop the partner String with every result. But outside the loop java isn't recognising that String, bcs...ofc...it was declared for the loop. So i had in every if or else if clause a " String partner = anwsA;" and answB and answC, thats why i cant declare them outside.
Short: i need a partner String variable that could have 3 possible results... i jut need one
I'm using Eclipse Luna 4.4
View Replies
View Related
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
View Related
Jul 16, 2014
2. Declare a class called Quadrilateral that can be used to represent a quadrilateral. What instance variables are required? This class should include the following methods:
• Accessor and mutator methods. Notice that negative and zero lengths should not be accepted.
• A method called isParallelogram that returns a Boolean value indicating if the quadrilateral is a parallelogram.
• A method called isRectangle that indicates if the quadrilateral is a rectangle. This method should invoke the method isParallelogram and return a Boolean value.
• A method called isSquare that returns the Boolean value “true” if the quadrilateral is a square. This method should invoke the method isRectangle and return a Boolean value. URL...
import java.awt.Point;
public class Quadrilateral{
private Point p1, p2, p3, p4;
public Quadrilateral (Point p1, Point p2, Point p3, Point p4) {
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
this.p4 = p4;
}
}
that is all i got, my professor has never mentioned on anything about quadrilateral,and i have 0 knowledge about this.
View Replies
View Related
May 8, 2013
I have a doubt regarding to java.
Can we declare a class inside an interface?
Ans :yes
But what will be the situation?
I am not getting why it is required...........
View Replies
View Related