98 Posted Topics
Re: One method you can use is to start off with 2 integers that store the beginning of the Fibonacci sequence and loop through to modify them and iterate through the sequence. If one of the values exceeds the number you are checking, you passed it, so you know it's not … | |
Re: You have the right idea, but the main problem is in your equals() method that you created. Your method just checks to see if the inputted object is a date object. So for instance, calling if(check1.equals(check2)) will basically check if check2 is an object that is also a date. Since … | |
Re: You need to clarify what you want better and specify the place in the code where you are having an issue. However, this might help you: [CODE=Java] Student s; /* ...More lines of code here... -- s may have been initialized or it may still be an empty pointer */ … | |
Re: I think what BestJewSinceJC meant was that you should be more specific with what is going wrong with your code. Are you compiling it and an error message shows up on a line? If so, what is the message? Or is it that your code compiles fine, but it crashes … | |
Re: Wow... lots of errors... The first thing that strikes me is that you have several displayResults() methods, all with the same signature (double, double). It doesn't matter that you named the double variables differently in each displayResults() method -- Java identifies a method based on its name and parameters. You … | |
Re: double values tend to do that. I recommend having a method round the double value before printing it, so you only print a certain number of digits after the decimal point. A way I usually like to set the number of decimals is to multiply the double by 10^(number of … | |
Re: Agreed, class Class is messy business -- you need to be sure you know what you are doing. Personally I like to use the equalsIgnoreCase() method instead of the equals() method for strings simply because the user might enter "Yes" or "yes" and you will want to evaluate such answers … | |
Re: Um, this isn't our assignment. And it's not your place to give commands like "reply to me immediately" if you are the one who needs help. Sorry kiddo, you'll have to figure this one out on your own. If you have a specific question we may be able to help … | |
Re: On my computer it seems to be working fine. You should generally use the print command instead of printf -- it is much easier to read. Basically you just include whatever pieces you want and put them together with + signs: System.out.print("The Remainder is: " + answer + "\n"); You … | |
Re: Also if possible try to indent your code. It makes it much easier to read if each block of code is indented by 4 spaces (unless BBcode has a tab tag). | |
Re: A multidimensional array IS an array of arrays. A grid is basically an array running up-down with each element containing a left-right array. [ICODE]int[][] multiArr = new int[5][5]; //creates array of 5 elements, each element containing an array of 5 elements. (5x5 grid)[/ICODE] | |
Re: Welcome to Daniweb! If you want to post code in the future, please use the code tags (read the 2 announcement posts at the top of the forum). The code tags just make the code easier to read. As far as sorting is concerned, you have 2 options: you can … | |
Re: Lets say you are using an integer as a counting variable: [CODE=Java] int x; for (x=0;x<10;x++){} //remainder of your code here [/CODE] If you want to access the value of x later the for-loop may not have been evaluated if the condition was [I]Never [/I]met, so x would be lacking … | |
Re: The first step is to write the Sandwich and Customer classes. These are in my opinion the best procedure when writing a class: 1. Define the various components of the state of the object (its properties). Set them all as private variables. 2. Create accessor and mutator (get + set) … | |
Re: Also, please use code tags to make your code more readable. | |
Re: What he said. Between every { and } tab all lines 1 tab inward. Also you should note that on line 34 you will get an error because you can't call a variable "class" since the word "class" is a reserved Java word. | |
Re: I think you are making this way too complicated for yourself... [CODE=Java] int count = 0; int[] arr = new int[10]; int num = 0; while (count<10 && num!=-1) { num = Integer.parseInt(JOptionPane.showInputDialog("Enter Integer Number "+(count+1))); if (num!=-1) arr[count] = num; count++; } [/CODE] Shouldn't that do the trick? | |
Re: ...If you haven't ever touched a Java program, shouldn't you be starting with HelloWorld.java? | |
The commonly used syntax: [CODE=Java] public class Foo { private int x; public Foo(int x) { this.x=x; } } [/CODE] Is actually "flawed." If you have an anonymous inner class inside the Foo constructor (i.e. if this were a GUI code and you were adding an ActionListener) then the private … | |
Re: If you read it in as a string, and then use substring() and charAt() methods to isolate certain strings and characters, then you can just confirm that the 3 substrings are integers and the 2 characters at the designated points are '-'. If this is the case then you can … | |
Re: You would need to post the rest of your code for anyone to help you out with that piece. | |
Re: We can point you in the right direction, but first we need to see your attempts to figure it out. You can't just say "I don't know" with your paper clean. Play around with it and see where you get. If you don't get anywhere we'll help, but we need … | |
This program creates 2 methods for convenience: menu() and binaryQ(). The menu method accepts several strings as its parameters. The first of the strings is the question to be displayed at the top of the menu. The following strings are the menu choices. For instance, if you call [icode]Confirmer.menu("What is … | |
Re: Ok so in that problem they want you to use something called method overriding. What this means is as follows: If a superclass (Transaction) has a method purchase(), then by default its subclasses (Rental, Return) will also have that same method. If I say: [CODE=Java] Rental rent = new Rental(); … | |
Re: There are a couple of things you can do: 1. Make another class that has an array of these objects, each representing a single student. Then have methods of that new class have the class average, min, and max methods, which basically takes the scores from all the GradingScaleRP1 objects … | |
Re: The command System.exit(0); will kill your program. So if you have the lines: [CODE=Java] if (val == testNum) System.exit(0); [/CODE] Then your code will exit if the testNum is entered. Note that this is not like breaking from the loop -- your program will basically jump to the end of … | |
Re: Value comparison is done with the == operator. For instance, the expression (5==5) will return true. This operator works with all numerical values as well as with characters. To compare strings, use the syntax (str1.equals(str2)). For instance, ("apple".equals("orange")) will return false. | |
Re: Look at these 2 sites: [URL="http://java.sun.com/j2se/1.3/docs/api/java/awt/event/KeyListener.html"]KeyListener API[/URL] [URL="http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html"]Tutorial for KeyListener[/URL] In general, KeyListener works exactly the same as ActionListener, except instead of overriding actionPerformed() you need to override keyPressed(), keyReleased(), and keyTyped(). Play around with this interface and see what results you get. | |
Re: It's funny -- I had the same confusion a little while ago. I am fairly sure this is how it works --> algorithms have different runtimes based on their designs, and each of those runtimes can be roughly estimated depending on the algorithm type. Each of these estimations can be … | |
Re: [QUOTE=PatrickSharp;1139849]Since code isn't your thing.. here is some psuedocode: [CODE] set valid input false while (not valid input) do prompt user for input value. get input value from user. if input value is what you are looking for set valid input true else display error message end [/CODE] That is … | |
Hey everyone! If I have a matrix as follows: int[][] mat = new int[5][3]; Does that mean that the row size is 5 and the column size is 3 or vice-versa? I am really confused about whether the order is [row][col] or whether it is [x][y] in relation to the … | |
Re: So basically a method works like a machine that accepts some input and does something based on that input. Usually it is useful for organizing your code so you don't get a crazy-long main function like the one you ended up with. I shortened your main function using some methods, … | |
Re: I imagine your assignment was to use for-loops for the array. Ok, so basically an array is a list of items (in this case integers) and each is assigned a certain index. You seem to understand the basic usages of an array, as your syntax in that respect is correct. … | |
Hey everyone~! I am having a major issue understanding class casting. I thought I understood it, but I found 2 problems in my Barron's book that seem to contradict in their explanations. Can someone please explain the distinction between these two problems??? [B][U]Problem 1:[/U][/B] [INDENT] Bird = Super class Owl … | |
Re: I would start by creating a person object with those pieces as private variables. Have one of those private variables be the array of grades. Make appropriate getters, setters, and constructors. Make a method to get the grades and store them in the array. Make a method to retrieve the … | |
Re: Well since you have a /* at the beginning you need to end it with a */ or else your whole code will be one giant comment ;) | |
Hey everyone~! I am pretty new to threading but I'm trying to figure it out by playing around with its possibilities. I wrote a program where 2 threads are supposed to "race" to count to 10 and then the first one there says "I win!" and the second says "I … | |
Re: Basically I think what is happening is this: you have a switch statement with the user's choice -- say they select choice 1. Then it goes to case 1. Case 1 has 2 commands: Add(); and break; First Add() is executed. When add() is finished, the second command is executed … | |
Hey everyone~! I have been studying for the AP Computer Science exam, and I have a question where I think Barron's is wrong, so if someone could confirm whether I am correct or not please let me know! The following code snippets are given: [CODE=Java] public interface Player { /* … | |
Re: Um... I have no idea how you intend to hook up the webcam with java... that sounds really advanced. However, I would recommend serializing your database each time the program closes and deserializing it each time it opens. Check out the serializable interface on the Java API. | |
Re: The first step is to make sure you fully understand the concept of an [URL="http://java.sun.com/j2se/1.4.2/docs/api/java/util/ArrayList.html"]arraylist[/URL]. Once you do, check out this code -- I think the simplest approach is to use an arraylist of arraylists to represent the theaters: [CODE=Java] import java.util.ArrayList; public class Cinema { public static void main(String[] … | |
Re: I'm not really sure what you are trying to do, but one thing that might help you is a for-each loop. The syntax is as follows: [CODE=Java] for (One instance of a type : A collection of that type) { //Iterate through the list. } [/CODE] For example, [CODE=Java] ArrayList<String> … | |
Re: Here's something to think about to jumpstart your forming of an algorithm -- consider using nested loops. Have the outer loop keep track of the row and each time it completes have it jump to the next row, and have the inner loop print the contents of the row. Think … | |
Re: I can't see the rest of your code, but it's possible that the string has a null value and thus doesn't have a length of zero. There is a difference between an empty string and a null string. An empty string is like a thread with no beads on it. … | |
Re: I think I might know the issue. If you are saying in your code System.out.println(num1+num2+num3) to print the 3 numbers, your error is here. The print method is adding the integers together and then is printing them. If you add the integers to an empty string and print it as … | |
Re: Every method call needs to match the way it was set up. Since you have a method called "void reverse(int units)", the method call is going to need to be set up so it enters a number of units and returns nothing. If you say System.out.println(myCar.reverse()), you are going to … | |
Can someone please write a piece of code for the following and comment each line (except for really simple ones like int x = 5;) explaining what it does? Objective: write a program that "spins a number wheel" in the background while waiting for you to type "stop" -- when … | |
Hi everyone! This code runs without throwing exceptions and stuff, but it doesn't exactly run when I click the "Submit" button -- what is supposed to happen is that I click it and it generates a random number. What happens is either nothing or if I get lucky and click … |
The End.