Anyone know Java?

TheSomeone

Newbie
Joined
Dec 24, 2004
Messages
2,186
Reaction score
0
What are static methods exactly? And unqualified names? And what's the default package?

AUGH! I"M DYING.
 
I think this thread would be better suited in the coding section...
 
static methods are methods that don't require the class they are a member of to be instantiated as an object. I don't know java syntax, but here it is in C#:

Code:
public class Class1
{
     public void NonStaticMethod()
     {
           // do stuff
     }

     public static void StaticMethod()
     {
           // do stuff
     }
}

So then, you call them like this:

Code:
// Calling the nonstatic method requires an instantiated object:
Class1 objClass1 = new Class1();
objClass1.NonStaticMethod();

//Calling the static method does not:
Class1.StaticMethod();

Get it?
 
no problem... from what I hear, Java and C# are very much alike, at least from an OOP perspective... if you have any more questions I won't mind being PM'd
 
ahh java... the first language I ever learned.

A package is basically a group of classes, every class has to be in a package stated at the top of each source file, so if the package the file is in is not stated the class is said to be in the default package.
 
JellyWorld said:
ahh java... the first language I ever learned.

A package is basically a group of classes, every class has to be in a package stated at the top of each source file, so if the package the file is in is not stated the class is said to be in the default package.

Same thing in C#... except microsoft calls them "Namespaces"
 
Circumference.java:28: operator * cannot be applied to java.lang.String,int
System.out.println("The diameter of the circle is - " + (radius * 2));
^

this makes no sense why cant i use * :\
 
got it to compile buttttttttttttttttttt

Enter the circles radius:
7
The diameter of the circle is - null.
The circumference of the circle is - null.
The area of the circle is - null.

it wont read my math functions kaj;sflkasjgas
 
xLostx said:
Circumference.java:28: operator * cannot be applied to java.lang.String,int
System.out.println("The diameter of the circle is - " + (radius * 2));
^

this makes no sense why cant i use * :\

Because radius may be a string or some other non numerical type, and you're trying to apply a mathematical operator to it, which is impossible. For all it knows, you're trying to do something like:

Cake * 5000

As tasty as that may be, its just not possible, unless of course "Cake" is a numeric variable.

Here's how it would have to be done in C#:

Code:
int radius = 15;
Console.Write("The diameter of the circle is - " + (string)(radius * 2));
 
import java.io.*;
public class Circumference
{
static class Circum
{
float radius;
double diameter;
double circumference;
double area;

public Circum() throws IOException
{
BufferedReader inData =
new BufferedReader(new InputStreamReader(System.in));
float radius;

System.out.println("Enter the circles radius: ");
radius = Float.parseFloat(inData.readLine());

final double PI = 3.14159;

double diameter = (radius * 2);


double circumference = (PI *(radius * 2)) ;


double area = (PI * radius);

}

public void printCircleInfo()
{
System.out.println("The diameter of the circle is - " + diameter);
System.out.println("The circumference of the circle is - " + circumference);
System.out.println("The area of the circle is - " + area);
}
}

public static void main(String[] args) throws IOException
{
Circum text;
text = new Circum();
text.printCircleInfo();
}
}
why doesnt this work it just returns 0.0 when its executed, im too tired to think
 
xLostx said:
why doesnt this work it just returns 0.0 when its executed, im too tired to think

Why does it have to be so complicated? Are you supposed to show you can use various methods and stuff?
 
From: Programming and Problem Solving with Java

* Page 150, Problem 4. Use a default constructor for inputting the data needed and doing the calculations, and method printCircleInfo() for printing the result.



Your program must follow the requirements for documentation, test plans, coding style, and comments. See documentation_standards.doc
 
text could be anything, i just use it for declaring a name or something and then instantiation
 
Heh, I'm in Java II right now and I need some more practice.

Here's what I came up with if you NEED a constuctor:

ME :D said:
import java.io.*;
public class Circumference
{
public static void main(String[] args) throws IOException
{
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));

float radius = Float.parseFloat(inData.readLine());

double diameter = (radius * 2);
Circum(radius);

final float PI = 3.14159;
double area = (PI * radius);

System.out.println("The diameter of the circle is - " + diameter);
System.out.println("The circumference of the circle is - " + circumference);
System.out.println("The area of the circle is - " + area);
}


public static double Circum(float x)
{
double circumference;
final double PI = 3.14159;

circumference = (PI *(x * 2));
return circumference;

}

}

BTW....I don't have my stuff installed on my comp to compile or run it so I didn't test it or anything....

And now I see you said default constructor and I'm silly :p
 
let me take a gander...forgot to add input from the user, cant find the radius without input

Circumference1.java:18: cannot find symbol
symbol : variable circumference
location: class Circumference1
System.out.println("The circumference of the circle is - " + circumference);
^
1 error
 
woooooo got it to work :D:D:D ended up with this -

import java.io.*;
public class Circumference1
{
public static void main(String[] args) throws IOException
{
BufferedReader inData = new BufferedReader(new InputStreamReader(System.in));

System.out.println("Enter the circles radius: ");
float radius = Float.parseFloat(inData.readLine());

double diameter = (radius * 2);
Circum(radius);

final double PI = 3.14159;
double area = Math.PI*radius*radius;

double circumference;

circumference = (PI *(radius * 2));


System.out.println("The diameter of the circle is - " + diameter);
System.out.println("The circumference of the circle is - " + circumference);
System.out.println("The area of the circle is - " + area);
}

public static double Circum(float radius)
{
double circumference;
final double PI = 3.14159;

circumference = (PI *(radius * 2));
return circumference;

}

}
 
why aren't you using your method to calc the circumference, and also why are you using math.pi when you already have a final double pi just above it?
 
i had 2 files on the go and my original one was using Math.PI but I switched it back to PI since it was a declared constant, things look a bit diff now
 
Have you tried this:

Code:
double circumference;

circumference = Circum(radius);

That way you would make use of your method, but if you arent going to use it then I would just delete the method as you don't really need it.
 
Back
Top