DavidKroukamp commented: but true anyhow +7
stultuske commented: nothing against using IDE's, but it's better not to depend completely on them. btw: mijn nederlands is hoogstwaarschijnlijk ook beter dan mijn Engels :) +12
I see, never mind :)
No, I dont agree.
First of all, you are creating a constructor which returns a String. That is not possible, all constructors are voids.
Next, toString() methods should just return a straight string, why would you call a Constructor?
public class TestProg2{
public static void main(String args[]){
String filler1="Rank";
String filler2="Suit";
Card card = new Card();
System.out.println(card);
}
}
class Card{
public String Card(){
String test="of";
return test;
}
public String toString () {
return "This is the toString() method in the Card Class";
}
}
Ah, overloaded constructors. A question from my side, how much does your child know about overloading?
I'll try to explain:
We have a class, Box, and we want to have three constructors for it, one for each possible way to make class. The first possibility is a box of which we don't know any sizes, the second possibility is a box where we know that all sizes of the sides are equal (a cube!), and the third possibility is a box where we know all the sides, and they all have a different size.
Now we make the class and the three constructors for our class:
class Box {
Box () {
}
Box (int a) {
}
Box (int a, int b, int c) [
}
}
When we have our class, we are lazy. Because we don't want to type our initializing code three times, we call our last constructor from the first two constructors. We will just pass along some extra values. For the first box, we assume all the sizes are 1, because no information is given. For the second constructor, we can call the third using the given size for each side:
class Box {
Box () {
System.out.println ("I'm a box, with unknown sides.");
this(1,1,1); // equal to calling this(1), although this is directer
}
Box (int a) {
System.out.println ("I'm a box, with all sides "+a+".");
this (a,a,a);
}
Box (int a, int b, int c) [
System.out.println …
Integer.toHexString(myInt)
Try new Boolean(false). Notice the capital letter. boolean is a primitive type, while Boolean is an object.
If you give an object in stead of a String in a method, the object's toString() method is called.
In your example, this is happening in the line
System.out.println(b); // You pass an object instead of a String
.
What really is executed:
System.out.println(b.toString());
Please, use the following for your labels:
JLabel [] labels = new JLabel[20];
MyClass() {
setLayout(new FlowLayout());
for (int i=0; i<labels.length; i++) {
labels[i] = new JLabel (String.valueOf (i+1));
add(labels[i]);
}
}
Oh! I'm sorry, I thought I said 'if'. (I'm Dutch, so my English is not really good.)
Yes, if you indent your code manually, you'll find out eventually. I still think starting programmers should learn to code with IDE's, because they'll learn a lot faster, and never forget little stupid problems you would make in Notepad.
Maybe, that's why I said 'if'.
I always encourage people to start programming with a good IDE (no, let's not start a discussion which is the best), to avoid these kind of mistakes.
Use Ctrl+Shift+F when you work in Eclipse, this will format your code. It's very useful when you don't know where you missed a bracket. The formatter will space out the code in the wrong way when you missed one, so you can see where you made an error!
I made this in a few minutes, I hope this is the idea you want. You can edit anything on the JPanels.
Main class:
import javax.swing.*;
public class Test extends JTabbedPane{
public static void main(String[] args) {
JFrame f = new JFrame("Test");
f.setVisible (true);
f.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
f.setBounds (100,100,800,400);
f.add (new Test());
}
Test() {
add(new Tab1());
add(new Tab2());
}
}
Tab1:
import java.awt.*;
import javax.swing.*;
public class Tab1 extends JPanel{
Tab1() {
setLayout (new BorderLayout());
add(new JTextArea (), BorderLayout.CENTER);
add(new JButton("Hi!"), BorderLayout.SOUTH);
}
}
Tab2:
import java.awt.*;
import javax.swing.*;
public class Tab2 extends JPanel{
Tab2() {
setLayout (new BorderLayout());
add(new JTextArea (), BorderLayout.CENTER);
add(new JButton("Bye"), BorderLayout.SOUTH);
}
}
It shouldn't, if the tabs are all different.
You only need to write the methods that need to be changed, compared to the normal JPanel.
All the buttons/textFields can be added, just like you want, what do you want to add in the subclasses?
Yes, but I think it will become a lot easier.
Make 1 class, which extends JFrame (or maybe a JPanel you put on the JFrame), and three other classes that extend JPanels. You add those three classes to the first, just like you are doing now.
Now in each of those classes, you can use a Layout manager, to control how all the elements are spaced out in the JPanel. You can also specify it yourself.
For more help, I would need to know what exactly you want in your tabs, and how the input fields should change (change position, become invisible?).
Why are you not using JPanels as tabs? You can just add those to the JTabbedPane, and add any components to each tab you want!
Yeah, that's better, but usually it complicates things when you start changing other peoples code (maybe there is some requirement), if the code is correct.
Cannot Find Symbol is an error when you try to call a method or variable that does not exist.
I think you need anObject.getClass().getName(), just like you did previously.
Great, good job! I probably should have called it something else, but that's hard to describe...
If I find out why the second problem is there, I'll tell you (haven't found out yet)!
Inside the Set class:
public void removeObject(T anObject)
throws CannotRemoveException
{
String paramClass = anObject.getClass().getName();
String currClass;
int removalCount = 0;
T removeObject = null; // removeObject is my temporary object (not an official name)
// Logic ...
for (T currT: theList)
{
currClass = currT.getClass().getName();
if (currClass.equals(paramClass))
{
if (currT.equals(anObject))
{
removeObject = anObject; // set removeObject as anObject
System.out.println("Member found and has been removed from "
+ "the set.");
removalCount++;
}
}
}
if (removeObject != null)
theList.remove (removeObject); // remove the object from the list, when the loop is finished.
if (removalCount == 0)
{
throw new CannotRemoveException();
}
}
(I hope for your instructor that he has not written this part...)
Ah, I found it!
I have run into that problem a while ago, they should make a seperate Exception for it:
You are trying to remove an Object, from a Set/List you are iterating over:
for (T currT: theList)
{
currClass = currT.getClass().getName();
if (currClass.equals(paramClass))
{
if (currT.equals(anObject))
{
theList.remove(anObject);
System.out.println("Member found and has been removed from "
+ "the set.");
removalCount++;
}
}
}
You are trying to remove anObject, from the Set/List in the for loop.
To solve it, I made a temporary object, and removed the object from the List/Set when the loop is done.
I haven't yet found out why there is no exception when the user enters a name that is not in the list, but I'll look into that.
Looks great!
Please mark the thread as solved, if it is solved!
Well, to me the posted code look about right, I need more code, especially the attack() method, to see what goed wrong.
Also:
Pokedex[a] = Pokedex[i];
Pokedex[b] = Pokedex[i];
You are putting the two pokemons in the beginning of the list? There might be pokemons there, that you overwrite.
You are right. I didn't understand getCurrent() when I saw it...
(The name doesn't really describe it's function)
Could you post a zip with all the source files in it? I would like to test the program myself, I can't see what the problem is from this code.
Great!
If this thread is now solved, please mark it as solved!
OK, lets go through it.
<html>
<head>
<title>Vaja 6.2</title>
<style>
.cell {
background-color: #000;
}
</style>
<script type="text/javascript">
function enterSize()
{
x=prompt("Enter width:")
y=prompt("Enter height:")
for(var i=0; i<x; i++)
{
document.write("<table id=table border=1 cellpadding=0 cellspacing=0>")
document.write(" <tr>")
for (var j=0; j<y; j++)
{
var maxVal = x>y ? x : y;
document.write(" <td width=40 height=40 class='cell' id='cell" + (i*maxVal+j) + "'>")
document.write(" </td>")
}
document.write(" </tr>")
document.write("</table>") }
}
</script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body >
<div><script>
enterSize();
var maxVal = x>y ? x : y;
for (var i=0; i<x; i++) {
for (var j=0; j<y; j++) {
if ((i+j) %2 == 0)
$('#cell'+(i*maxVal+j)).css('background-color', '#0F0');
else
$('#cell'+(i*maxVal+j)).css('background-color', '#FFF');
}
}
$('.cell').click(function () {
if ($(this).css('background-color') == 'rgb(0, 255, 0)')
$(this).css ('backgroundColor', 'rgb(255, 0, 0)');
else if ($(this).css('background-color') == 'rgb(255, 0, 0)')
$(this).css ('backgroundColor', 'rgb(0, 255, 0)');
else if ($(this).css('background-color') == 'rgb(255, 255, 255)')
$(this).css ('backgroundColor', 'rgb(0, 0, 0)');
else if ($(this).css('background-color') == 'rgb(0, 0, 0)')
$(this).css ('backgroundColor', 'rgb(255, 255, 255)');
});
</script></div>
</body>
</html>
What have I done:
- I made a maxVal for the problem with all the squares when y was bigger than x, and used that instead.
- I used jQuery for handling the clicks. The css function returns an rgb string, so you have to use that.
If you want to write some javascript yourself, go ahead, I like this.
Also, I reccommend trying to understand what I have done. Maybe you will learn something from it?
Like I was saying, you are manipulating the background in three different ways.
Choose 1 of them, so it will not become confusing. Then, think about what you want to change, because that will become very easy.
I do want to help you, but what I am also saying is that if I put down all the code you need, you are not going to learn anything, because the next time you run into this problem, you still won't know the answer...
If you think this thread is finished, please mark it as solved!
Please mark the thread as solved if it is solved!
OK, you are now mixing three different types of changing the background colour.
- In the HTML, (bgcolor=grey)
- In the jQuery ($('#cell'+(i*x+j)).css('backgroundColor', '#0F0');)
- In your method (sivo.style.backgroundColor = "red";)
I would stick to either plain CSS, jQuery(using $(*).css(), your Javascript (using *.style.*), or HTML. The easiest for the things you want is still jQuery, but plain Javascript you type yourself will also work.
Anyway, I'd definately leave out the HTML, and code that part in CSS.
@skracer
Look into your code, and all our comments. Start learning, and dont copy our code. You should be able to find the problem by trying, and more trying, until it works! Programming is never going to itsself, you have to put some time in it!
Formulate the problem.
What do you have, what do you want, so what needs to change. Change exactly that...
if ((i+j) %2 == 0)
$('#cell'+(i*x+j)).css('backgroundColor', '#0F0');
In the above code I check for i+j%2 == 0. This is only valid for situations with a square board, or a board with x=k*2+y, where k is any whole number.
Think of something yourself that is valid for any y, probably a bit harder, but definately manageble I think.
OK, modified it a bit:
<html>
<head>
<title>Vaja 6.2</title>
<script type="text/javascript">
function enterSize()
{
x=prompt("Enter width:")
y=prompt("Enter height:")
for(var i=0; i<x; i++)
{
document.write("<table id=table border=1 cellpadding=0 cellspacing=0>")
document.write(" <tr>")
for (var j=0; j<y; j++)
{
document.write(" <td width=40 height=40 class='cell' id='cell" + (i*x+j) + "'>")
document.write(" </td>")
}
document.write(" </tr>")
document.write("</table>") }
}
</script>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body >
<div><script>
enterSize();
for (var i=0; i<x; i++) {
for (var j=0; j<y; j++) {
if ((i+j) %2 == 0)
$('#cell'+(i*x+j)).css('backgroundColor', '#0F0');
}
}
</script></div>
</body>
</html>
Please start thinking a little for yourself. I'm sure you could have managed to think of this yourself.
My idea: make a jPanel for each card, and use an Affine Transform object to:
- Reduce the width of the panel to 0
- Paint the other image
- Enlarge the width of the panel to the previous width
Like James said, you'll need to some some timing.
jQuery is not hard to learn (Tutorials and download: http://jquery.com/).
Look into some tutorials, and see what works out and where the problems come up, I'll be able to help you with that.
The ID generation in the code can be done like this:
for (var j=0; j<y; j++)
{
document.write(" <td width=40 height=40 id='"+i*x+j+"'>")
document.write(" </td>")
}
A counter is the variable in the for loop (a in the first, b in the second, c in the third).
I would suggest the following, made from your code:
for (int a=0;a <= 5; a++){
for( int b= 0; b <= 5; b++){
println("b = " + b);
if(b ==5)
println("loop terminated " + a);
}
}
Why do you have the inner loop with c?
You don't do anything with b, and all you will get Loop terminated 0-5 printed 6 times.
What exactly do you want?
I think I see the problem. When the user presses on Remove object from list, the method has a loop.
However, everytime you ask an object from the list, you call getCurrent(), nothing to do with the loop.
I hope this will solve it!
You are asking US to make it look better/work better, so that you get a better grade for working harder? You gotta be kidding me.
If you have a question please ask it, but we don't do any homework assignments around here.
If I remove the statement, the method does not return anything
It should, I think you may be removing something else there then the print statement.
I don't really understand what you want.
Could you explain that a little more?
I thinking I'd use jQuery to select each square, and change the color. In your for loops you can generate IDs for your squares.
Wow, that's new for me. I have never needed it before. In Eclipse, there was also no import warning, so where does that method come from?
There is no 'printf()' in Java. This is C++ syntax.
Use
System.out.println("Sum is " + sum);
or
System.out.println(String.format("Sum is %f", sum));
is you want to format.
Also, be careful using (i-2)/i. i is an Integer, and (i-2)/i will always result in 0 if you don't explicitly tell Java to use it as a float (like this: ((float)i-2)/(float)i).
Changed code:
public static void main(String[] args) {
float sum=0;
for (int i=3; i<=99; i+=2) {
sum += (((float)i - 2) / (float)i);
}
System.out.println (String.format (" sum %f\n", sum));
}
Good luck.
Make sure you take the width and the height of the panel, when the panel have been initialized. In the constructor of the panel, both the width and the height are 0.
Polymorphism
Polymorphism is the capability of an action or method to do different things based on the object that it is acting upon. This is the third basic principle of object oriented programming. Overloading, overriding and dynamic method binding are three types of polymorphism.
From: http://home.cogeco.ca/~ve3ll/jatutor5.htm
Sounds to me like it is. Overloading is one of the three forms...
Write a small program, implementing keyListener. On the key pressed method, you print out te character.
1. Simple: yes!
2: Not that I know of. The methods you make are just as they are on runtime, only compiled.
AddFilm():
//Add Film Method
public static void addFilm(String code, String title, String description, String language, int time, String production, String status, Film[] callFilms){
if(numberFilm == 0){
callFilms[numberFilm] = new Film(code, title, description, language, time, production, status);
++numberFilm;
}
else{
Film addFilm = new Film(code, title, description, language, time, production, status);
if (!checkFilm(addFilm)){
callFilms[numberFilm] = addFilm;
++numberFilm;
}
else
System.out.println("You've Entered Duplicate Data. Please Re-Enter Again.");
}
}
where checkFilm is the following:
public static boolean checkFilm(Film addFilm){
boolean check = false;
for(int i = 0; i < numberFilm; i++){
if(callFilms[i].equals(addFilm))
check = true; // don't make check false if there is no duplicate, only update the variable if there is a duplicate, or the data will get lost.
}
return check;
}
Why would you use other variables then the ones you need? It makes code messy, and it it totally unnecessary.
I think you need to try a little yourself, and see if it works.
Oh! I see my error now!
I thought: First the adding, then the loop. It is the other way around.
What I dont understand: why would you do a check, if the film has not yet been added? You should do a check after the flm has been added, and if there is a duplicate, remove it.
You can also do a check before the adding, but you'll have to make the new Film(...) first, and pass that into the checking method. The new Film(...) should then be compared to all the films already in the array.