Changing Ints From Another Class
Jun 27, 2014
I am making a simple text based game and i have a monster, and I am tring to make its health go down whent he user input "s" but istead it doesnt change and the users health even goes up.
Main.java
package exodus.game.main;
import java.util.Scanner;
import exodus.game.monsters.Nirav;
public class Main {
static String name;
static String inputtemp;
[Code] .....
View Replies
ADVERTISEMENT
Jul 10, 2014
I am starting to learn java , If suppose we write a simple hello world program
class helloWorld
{
public static void main(String args[])
{
System.out.print("Hello World !");
}
}
And save this as test.java.Now after compiling it a helloWorld.class file is generated.But if we compile the same after adding "public" in front of 1st line, it throws error.
public class helloWorld
{
public static void main(String args[])
{
System.out.print("Hello World !");
}
}
but then changing the file name to the name of the class i.e. helloWorld.java, corrects the error.
View Replies
View Related
Jun 6, 2014
So, Once again I'm attempting to make a Who Wants to Be a Millionaire GUI game. This is the actual game screen code
package WWTBAM;
import java.awt.BorderLayout;
public class GUIGame extends JFrame implements ActionListener {
public static int moves = 0;
public static boolean finished = false;
public static boolean correct = true;
[Code] ....
When ever try and change the buttons text outside of the actual GUIGame constructor i can't. Like in the main method or the action listener.
The code won't work if you copy paste because it's a part of a larger package.
View Replies
View Related
May 26, 2014
Create a dnd (dungeons and dragons) character creator and back ground generator, have it display and run on a gui, to start with i decided to creator the gui as i go so i can see the progress, first i tried eclipse and windows builder, well after 1 day of reinstalling windows builder in about 5 different ways from multiple guides and sometimes getting it to partly work ...
After doing some more research I have figured out how to get the main program to display the gui . Heres what i have:
package com.mrgreaper;
import javax.swing.*;
public class MainWindow {
private JPanel mainWindow;
private JLabel playerlbl;
private JLabel playerNamelbl;
[Code] .....
Now this works fine and the gui displays when the program is run but I can't change the text of any of the jlabels or textfield. If I try to do it in the main class i need to change them from private to public static but then in the form builder it says "cannot bind to static field *name of field*" but if i take static off then i cant change its value!
From what i understand this is because the window is an instance, so how to i change the value in that instance? I could put all my code in the one class, the one that creates the gui, but i really want it seperate, i would like the gui to update as the code runs... So how do i do it, how do i change the contents of jlabels on the fly, read the contents of text boxes on the fly etc....
I tried adding a getter setter
public void setCharFirst(JLabel charFirst) {
this.charFirst = charFirst;
}
in the MainWindow.class
Then i tried to set it from my main function
MainWindow.setCharFirst("test");
but it cant access it as my main function is static and it is not now can i make it static...
View Replies
View Related
Aug 10, 2014
Java Code:
package ZooZ;
import java.util.Random;
public class Animal {
int playerOne; <------- Remains 0
int playerTwo; <------- Remains 0
Random random = new Random();
[Code] ....
If you look at the code, I set "playerOne" and "playerTwo", to set them as what was passed in from another class..
In the Animal constructor.
in that constructor the value is 2. But when I use it in the "Bash()" method it is 0 in the console.
Why doesnt the playerOne and playerTwo stay the value it is assigned in the constructor.??
View Replies
View Related
Feb 5, 2014
public Date(String s) {
???????
}
such that
Date("1250/345/759")
it will return
(1250,345,759)
in short Date("a/b/c")
return (a,b,c)
View Replies
View Related
Jan 24, 2014
I've been trying to make a list of numbers/ints and later on check that list for a specific number.
View Replies
View Related
Nov 5, 2014
I have started learning Java and have some across some difficulties. I'm trying to subtract two strings.
for example, with these strings;"032"&&"100". I want to be able to subtract each number individually so that the answer would be "032". 0-1;3-0;2-0;. if i get a negative number, the number would be zero. hence, the 0-1 staying zero in the answer.
I have tried using substring, and parsing the two values to ints, but don't know what to do next. I have also tries using a for loop, to go through each arrays of the strings. I am not allowed to use StringBuilder and I need to return "032".
Here's what i've done so far:
public static String appliquerCoup( String combination, String coup ) {
String nouveauCoup="";
if(combination!=null&&coup!=null){
for(int i=0;i>combinaison.length();i++){
[Code] ....
View Replies
View Related
Oct 31, 2014
I have been asked to construct an application that will read in the exam mark and coursework mark and then print out to 1 decimal place the average of the coursework and exam mark. When I submit it it says it's incorrect.
class Main
{
public static void main( String args[] )
{
int ExamMark=71;
int CourseworkMark=40;
double mark = (double)(ExamMark + CourseworkMark) / 2;
[Code] ....
View Replies
View Related
Oct 31, 2014
how to use Java and for my coursework I have been asked to construct an application that will read in the exam mark and coursework mark and then print out to 1 decimal place the average of the coursework and exam mark. When I submit it to the coursework submission system it says it's incorrect.
The answer that it's asking for is the answer that my code produces (ex = 71 cw = 40 mark = 55.5) But for some reason it says this when I submit it:
-#### << Differences between expected (<) your answer (>) >> ------------
1c1
< ex= 91 cw = 80 mark = 85.5
---
> ex = 71 cw = 40 mark = 55.5
-------------------------------------------------------------------------
Check the above output for why this attempt failed And when I change the ex to 91 and cw to 80 it asks for > ex = 71 cw = 40 mark = 55.5 again.
class Main
{
public static void main( String args[] ) {
int ExamMark=71;
int CourseworkMark=40;
double cost = (double)(ExamMark + CourseworkMark) / 2;
System.out.printf("ex = " + ExamMark + " cw = " + CourseworkMark + " mark = " + cost);
System.out.println();
}
}
View Replies
View Related
Aug 16, 2014
I have the following program. In a nutshell, I creates an array of 3 consecutive ints and the user has to guess what those numbers are, knowing that they are between 0 and 7, this is from Head First Java. In the book, the code has a bug that is made on purpose and they challenge you to fix it. As you can see bellow, every time a user's guess matches a int in the array, the NumOfHits is increased by one. When the NumOfHits is 3 (the length of the array) the game finishes.
The bug is that if you guess 1 and 1 is in the array, if you type in 1, 3 times, I will increase the NumOfHits 3 times and end the game as if you had won. To solve the bug, you need to find a way to tell the program that if the user already guessed that number, it should no longer be taken into account and we shouldn't increase the NumOfHits if the same number is provided.
I "fixed" this by searching for the index of the number that matches an int of the array, and changing that index's value to 100, since the user knows that the numbers are between 0 and 7, they will not submit 100.
The result? If the user submits 2 and 2 is in the array, the value of its indexed is replaced by 100, so that if the user submits 2 again it doesn't count as a match.
Please see the comments in the code
import java.io.*;
import java.util.Arrays;
class DotCom{
int NumOfHits = 0;
int[] LocationCells;
public void setLocationCells(int[] locs){
LocationCells = locs;
[Code] .....
The code works, but for some reason, some times I get a ArrayIndexOutOfBoundsException run time error and it appears to be somehow random. This is what I saw on the command line earlier
C:Userspablo.alvarez>java launchDotCom
Enter a number: 3
missed
Enter a number: 4
hit
Enter a number: 5
hit
Enter a number: 7
missed
Enter a number: 5
missed
Enter a number: 4
missed
Enter a number: 3
missed
Enter a number: 4
missed
Enter a number: 5
missed
Enter a number: 6
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at DotCom.checkYourSelf(launchDotCom.java:16)
at DotComGame.startGame(launchDotCom.java:59)
at launchDotCom.main(launchDotCom.java:72)
As you can see here, 3 returned 'missed' but 4 and 5 returned hit. Since the numbers are in sequence and are only 3, it makes sense that the last number is 6 (4,5,6). You will then notice that when I submitted 3 and 4 again it returned 'missed' as expected, because the numbers were already matched earlier, but then when I submitted 6, it returned the error seen above.
Sometimes this doesn't happen at all, sometimes it happens at the 2nd guess or third, it appears to be somehow random. I'm familiar with the error message, but I don't see where I'm using an index that doesn't exist in the array. The only time I'm referencing an array index is on
LocationCells[index] = 100;
but the index variable will only hold a value if the int submitted by the user matches on of the values in one of the indexes, so how is it that I'm going over the number of available indexes?
View Replies
View Related
May 14, 2014
How do I make it so that if both numbers are entered between 10 and 20 it will print false?
The assignment: Ask the user to enter 2 ints. Output "true" if one of them is in the range 10 - 20 inclusive, but not both. Otherwise output "false"
12 99 → true
21 20 → true
8 99 → false
15 17 → false
import java.util.Scanner;
public class Practice {
public static void main(String[] args) {
//get user input
Scanner user_input = new Scanner(System.in);
System.out.print("Enter a number: " );
[Code] ....
View Replies
View Related
May 13, 2014
I need to write a method that accepts an array of ints and squares each element of the array. No creating new arrays and no returning any values.
public void squareInts(int[] ints) {
for(int i = 0; i < ints.length; i++) {
ints[i] = (ints[i] * ints[i]);
}
}
View Replies
View Related
Sep 25, 2014
I am currently working on Java software which resize jpeg images and change DPI also. For JPEG images having app0JFIF node it works fine and the images new DPI is reflected in Photoshop. But if app0JFIF node not exist, I am trying to create a new one and set the DPI value there. Everything is going proper but if I open these images in photoshop it does not reflect new DPI but the size changes.
Java Code:
double dpi_in_inch = 0.393701 * newres;
File file1 = new File(imgName);
image = ImageIO.read(file1);
int wd, hi;
wd = (int) (newsize * dpi_in_inch);
[Code] ...
View Replies
View Related
Jul 16, 2014
My understanding was I could override a method from the superclass, including with different parameters, but when I try to use super. it gives me an error the arguments have to match the superclass. But, if I do that it won't make any sense.
The first code below is the superclass. The issue I'm having is on the second code at lines 7 and 10. The ultimate goal is to make a new class where I'm able to display various packages with or without insurance.
public class Package {
double shippingWeight;
public char shippingMethod;
final char air = 'A';
final char truck = 'T';
final char mail = 'M';
double shippingCost;
[code]....
View Replies
View Related
Aug 23, 2014
I am having trouble doing a simple exercise changing a for loop to a while statement, it works fine in the for loop but in the while statement it just prints out 10(it is supposed to count to 0 and print liftoff).
public void run() {
int i = 10;
while (i>=1) {
println(+i+"...");
i--;
}
println("liftoff...");
}
View Replies
View Related
May 1, 2014
I have a servlet with a method:
/**
* @return <code>double</code> Hours
*/
public double getDriveHours() {
return getAsDouble("DriveTime", 0.0D);
}
I would like to change the time to minutes. I will create a new entry in the db for the minutes, but wondered if the time minutes is also double.
View Replies
View Related
May 20, 2014
I'd like to make a simple cheat for an old game that's offline... It has no type of anti-cheat and I'd like to make a program that changes how much money you have... I think the first thing I have to do is get the data address or something like that by using cheat engine... How do I do that? After that how do I start using that info in my program and then send back a new value? Are there any classes that I should be importing and what methods do I use to do this?
View Replies
View Related
Jan 29, 2015
Color only changes, when resizng frame with mouse, not when clicking.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class SimpleGui1 implements ActionListener {
JFrame frame;
MyDrawnPanel drawnPanel;
[Code] .....
View Replies
View Related
Mar 17, 2014
im making a javabean for stopwatch which works perfectly, but i want in the property descriptor format, so the user can change its format.
currently i have;
private String displayFormat = "MMMM d, yyyy h:mm:ss";
private SimpleDateFormat formatter;
public synchronized void setDisplayFormat(String newFormat) {
String oldFormat = getDisplayFormat();
try {
formatter.applyLocalizedPattern(newFormat);
timerHasPinged();
[code]....
currently the timer works its format is in 00:00 minutes:seconds, but i want it to start as MMMM d, yyyy h:mm:ss, for example March 17 2014 00:00:01, so only one second has passed here. i believe the set and get format method is fine but the timerhaschanged needs to change as this is where the format takes place.
View Replies
View Related
Apr 24, 2014
Every time I load a txt file into a JTextArea it prints the results to the JFrame incorrectly I notice its mainly the white spaces this is happening to. I have tried a few ways to remedy this problem but it still keeps occurring? I've tried append() read() also setText() even Scanner. I have enclosed a picture of my GUI and my txt file I am using.
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import javax.swing.JFrame;
import javax.swing.JTextArea;
[Code] ....
Attached image(s)
View Replies
View Related
Oct 20, 2014
The Toys of the Future Company has decided to issue a new line of toy robots. While they are not sure exactly what their robots will look like, they do know the functionality that they will all have. All robots will be able to move forward, turn right, turn left, spin and detect an object in front of it via a sensor. The robot will use a touch sensor to determine whether it has hit an object in front of it, such as a wall. The robot will have two, three, or four wheels, but it will only be driven by two of them: a right wheel and a left wheel. Each wheel will be attached to a motor that controls it.
* ^ this is only something to think about
Produce a UML class diagram.
Using UML, describe the objects and classes that will be associated with this new toy. Either hand draw or use an appropriate UML creation tool to generate a UML design document for your Robot class. Create a set of classes that can be used to control the robot.
Develop your new class.
Create a new NetBeans project entitled, "Assignment_8_1" and develop your new class. Now, using the objects and classes that you have written, write a program for your robot that allows it to move in a large square. Allow your robot to run in a big two dimensional array and every time the robot encounters a square in your array, turn that square black.
So here is the code for the moving ball:
package ballapplet;
import java.awt.*;
import java.util.Formatter;
import javax.swing.*;
public class BallApplet extends JPanel {
private static final int BOX_WIDTH = 640;
[Code] ....
I want to know how to change the color of an object, and how to move it in specific directions.
View Replies
View Related
Apr 11, 2014
I'm having some trouble figuring out how to change the value of a variable that is passed into a class. I've tried just changing the variable, I've tried changing it by using the this.variable command, and I've even tried calling the setter class from within the class to change it, but it's still not working right. The first class is the one with Main in it and I just feed it some dummy data:
public class ExamConverter {
public static void main(String[] args) {
// TODO Auto-generated method stub
Age testAge = new Age();
[Code] .....
This is the other class to calculate the age the way we do it on psych tests - it might not be mathematically accurate, but it's what all the tables and such for raw to scaled score conversion are based on, so the math needs to be the same as opposed to "accurate" because of some months having 30 or 31 days, etc.
public class Age {
//==================Properties==================
// Variables for the test date and client date of birth
private int TestMonth;
private int TestDay;
private int TestYear;
private int ClientMonth;
private int ClientDay;
private int ClientYear;
[Code] ......
Based on this dummy data, the output is:
Test: 5/4/2014
DOB: 5/5/1971
Age Years: 43 Months: 0 Days: 0
However, it should be:
Test: 5/4/2014
DOB: 5/5/1971
Age Years: 42 Months: 11 Days: 29
View Replies
View Related
Nov 5, 2014
I have a empty JList in which I hit a button LOAD DATA which should load all the data. but once I load data i try to fill in the List but I keep getting errors.
String[] aos = new String[itrList.size()];
itrList.toArray(aos);
//JList listFAIL = new JList(aos);
//list = new JList(itrList.toArray());
//list.removeAll();
list.setListData(aos);
JScrollPane s = new JScrollPane(list);
I have tried doing setListData and i get a error;
And if i do new Jlist it doesn't change the data.
The List does fill as i have a checker for that.
View Replies
View Related
Jul 13, 2014
I have been working on a program that is meant to use a class' instructions in a program to add a value to a variable, save it, and present it. This is my class
public class Car
{
//FIELDS
private int yearModel;
private String make;
private int speed;
//METHODS
public Car(int carYearModel, String carMake)
[Code] .....
Whenever I call the accelerate method, a value of 5 is to be added to the speed variable. But whenever I call accelerate, it doesn't increase! I just don't understand why not. I've tried different renditions of adding 5 to speed and it doesn't quite work. I don't get any errors when I compile, just runtime, when it doesn't add 5 to speed.
View Replies
View Related
Jan 26, 2014
Ok, so in my quest to achive perfect ray casting and line/plane intersection, what I believe to be my last problem is precision of a float. I need some precision (0.000) but I am dealing with the difference of (-1.000000000) and (-1.000000002), where the second number would be completely off. I looked at DecimalFormat, but that just puts it into a string, I need actual loss of precision.
Java Code:
DecimalFormat form = new DecimalFormat("0.00");
String newX = form.format(x);
String newY = form.format(y);
String newZ = form.format(z);
x = Float.parseFloat(newX);
y = Float.parseFloat(newY);
z = Float.parseFloat(newZ); mh_sh_highlight_all('java');
View Replies
View Related