Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - nemo

Pages: 1 2 3 [4] 5 6 ... 82
46
then to go from any base to another...

Code: [Select]
import java.util.Scanner;
public class Test{
public static void main(String[] args){
Scanner intRead = new Scanner(System.in);
Scanner strRead = new Scanner(System.in);
System.out.print("Enter a number to convert: ");
String src = strRead.nextLine();
System.out.print("Enter the number's base: ");
int oldBase = intRead.nextInt();
System.out.print("Enter the desired base: ");
int newBase = intRead.nextInt();
String ans = Integer.toString(Integer.valueOf(src, oldBase), newBase);
System.out.println(src + " in base " + oldBase + " is " + ans + " in base " + newBase);
}
}

47
oops. i forgot setVisible(true).

yeah, you'll either want to make NPanel just implement MouseListener and all the methods in that interface, or make a new class which extends MouseAdapter and just overrides mouseClicked(MouseEvent e){/*your code on mouse click*/};
also, a hint, MouseEvent's have a method getX() and getY() to tell where the user clicked the mouse within a component (e.g. JPanel)

48
It just draws a grid on a single JPanel.

Yes, what I need to do is much harder, I have to draw 64 panels :S

modified. that code gives each panel a random color too.

49
yes, setLocationRelativeTo(null) is one of the few instances you pass null to a method.
and here's a simple demo that [maybe] draws the grid you want scout. i say maybe because i haven't tested it.


50
does this help at all? rarely should you call a method using null as the parameter.

you can do:
Code: [Select]
mainWindow.setLocation(x, y);
or
mainWindow.setLocation(new Point(x, y));

51
Correlation / Re: Request for a Java Programmer
« on: March 08, 2011, 06:54:30 pm »
yep, it's attached. if you need me to package it again into a .jar file, just ask and i'll do so.

52
^ Yes, that's another way.  The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();

i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a function
it doesn't have to have class in it at all.  If he wanted the class to be MyFunction, he should call it MyFunction, not MyClass.  a method is a function, yes.  But in this case it would be a method of the instance variable.  e.g.:
Code: [Select]
myToUpperCaseFunction.MyFunction();

yes, that's what i was aiming for. i just realized MyFunction isn't static so my code won't compile, but regardless if i saw a class called MyFunction i would be confused.

53
Computer Projects and Ideas / Re: [Project JAK]
« on: March 07, 2011, 06:53:36 pm »
what size sprites are you guys looking for? i made some ok-ish sprites you may be interested in, but they're 32x32

54
^ Yes, that's another way.  The only problem is that MyFunction isn't your constructor, and so anyone outside the class that tried to insatiate it might not call MyFunction();

i just copy/pasted scout's code. i'm not sure if he wanted the class to be MyFunction (shouldn't it be MyClass, then?) or if he wanted MyFunction to be a method, which i thought made more sense because a method is essentially a function

55
Computer Programming / Re: Looking for info...about java
« on: March 07, 2011, 06:49:42 pm »
java's a pretty easy language to pick up. here i'll describe the basics of OOP:
OOP is Object Oriented Programming. to start, i'd suggest to just learn about classes, objects, methods, instance variables and constructors along with how they interact. a class can be thought of a blueprint to make an object. a class contains methods (subroutines), instance variables (like A-Z+theta, except user-defined) and constructors (a special type of method used to create an object). here's a short example, labeling the parts of a class:
Code: [Select]
class Enemy{
     private int xPos = 0; // this creates an integer instance variable "xPos" and sets it to 0
     private int yPos = 0; // because of the variables are "private", they cannot be accessed outside the class

     public Enemy(int x, int y){ //constructor. enemies have an x and y position as parameters
          xPos = x;
          yPos = y;
     }

     public int getX(){ //this is a method. it returns a type int, and has no parameters passed.
          return xPos;
     }
     public void setX(int x){ //another method, with a void return type.
          xPos = x;
     }
}

to make and manipulate a new Enemy object, you would do:
Code: [Select]
Enemy myEnemy = new Enemy(5, 10); //creates a new Enemy object called myEnemy
int x = myEnemy.getX(); //calls the method "getX()"
System.out.println(x); //prints 5
myEnemy.setX(200); // calls method "setX()"
System.out.println(myEnemy.getX()); // calls getX(), and prints the value (200)

56
That's actually not entirely correct.  You can have many main functions, but only one is defined as THE main function that is called.

true. i've never seen an overloaded main function though, probably because main isn't a very descriptive name for any situation, really.

and scout, you cannot instantiate an object from a method. this is the proper syntax:
Code: [Select]
ClassName objectName = new ClassName(<param list>);
(yes, i know you can do ClassName objectName = new SubClassName(), i'm keeping it simple.)

graphmastur has corrected your code in one way, here's another way you could do it without changing the class name:
Code: [Select]
public class MainClass {
public static void main(String[] args)
{
MainClass myToUpperCaseFunction = new MainClass();
MyFunction();

}

public void MyFunction()
{
System.out.println("Hello");
}
}

57
Graviter / Re: Graviter - Axe
« on: March 04, 2011, 10:13:05 pm »
i like the first and second levels, especially the second. the third is really tedious, and i don't think it can be won. i can get past the first row and about half of the second.

58
Main isn't reserved, you can have a method called Main since java is case-sensitive. you cannot have another method "main" though. but you can have a variable "main" or an interface or class "main", though as Deep Thought said, it's not the best idea. the class name should reflect the type of objects the class is a blueprint for. it doesn't make sense to have a class called "Cereal" when the instance variables hold a string name, double GPA and an int gradeLevel. it makes more sense to name that class "Student".

59
i'm slightly confused. i know to make your code above work is declaring the method header of MyFunction as static, shown below
Code: [Select]
public static void MyFunction(){
}

you cannot "declare MyFunction as an object" as you put it. MyFunction is a method (java's word for subroutine or function) which cannot be instantiated as an object. in java, classes contain instance variables, 0 or more constructors, and methods. think of a class as a blueprint for an object. it tells the JVM how to instantiate an object.

i see you've found this site. its examples are well done and the site is helpful, though it lacks information about how OOP in java operates. Anyway, the problem with the code in the spoiler is that you're trying to make an object from class ToUpperCase, which does not exist because you have not defined it. if you notice, in the original example your code is from, the class name is ToUpperCase. you changed the class name to Main.
replacing
Code: [Select]
ToUpperCase myToUpperCaseFunction = new ToUpperCase();
with
Code: [Select]
window.setContentPane(new Main());

should fix the problem.

60
Computer Projects and Ideas / ACM programming contest
« on: March 01, 2011, 10:22:21 pm »
unless you're in college, you can't enter it, but i thought that some of the problems might appeal to some of omnimaga's computer programmers. they're part of an international collegiate programming contest, so they are anything but easy. you can find links to the "problem sets" here. i found a relatively easy problem here, as problem D, bipartite numbers.

Pages: 1 2 3 [4] 5 6 ... 82