PDA

View Full Version : Java Programming Question/Help



Kuruptix
01-22-2012, 06:38
This is my program that i am making




public class FreezeBoil
{
public static void printHeading()
{
System.out.println("Name");
System.out.println("CSC 201-01PR");
System.out.println("Freezeboil");
System.out.println("Project 1");

}

{
printHeading();
}

public static void main(String[] args)
{
final double FREEZE_PT = 32.0; // Freezing point of water
final double BOIL_PT = 212.0; // Boiling point of water
double avgTemp; // Holds the result of averaging
// FREEZE_PT and BOIL_PT
// Display initial data
System.out.print("Water freezes at " + FREEZE_PT);
System.out.println(" and boils at " + BOIL_PT + " degrees.");
// Calculate and display average
avgTemp = FREEZE_PT + BOIL_PT;
avgTemp = avgTemp / 2.0;
System.out.println("Halfway between is " + avgTemp + " degrees.");

}
}





When i run this in the compiler, the only thing that shows up is the "Water Freeze at" "and boils at degrees" text.


// Display initial data
System.out.print("Water freezes at " + FREEZE_PT);
System.out.println(" and boils at " + BOIL_PT + " degrees.");
// Calculate and display average
avgTemp = FREEZE_PT + BOIL_PT;
avgTemp = avgTemp / 2.0;
System.out.println("Halfway between is " + avgTemp + " degrees.");

I want it to also display the header as well which is


public static void printHeading()
{
System.out.println("Name");
System.out.println("CSC 201-01PR");
System.out.println("Freezeboil");
System.out.println("Project 1");


How do i make both of them show up?
thanks

iza
01-22-2012, 09:42
The only thing that gets executed is the main method. Your
{
printHeading();
}
isn't doing anything like that.


public class FreezeBoil
{
public static void printHeading()
{
System.out.println("Name");
System.out.println("CSC 201-01PR");
System.out.println("Freezeboil");
System.out.println("Project 1");

}

public static void main(String[] args)
{
printHeading();
final double FREEZE_PT = 32.0; // Freezing point of water
final double BOIL_PT = 212.0; // Boiling point of water
double avgTemp; // Holds the result of averaging
// FREEZE_PT and BOIL_PT
// Display initial data
System.out.print("Water freezes at " + FREEZE_PT);
System.out.println(" and boils at " + BOIL_PT + " degrees.");
// Calculate and display average
avgTemp = FREEZE_PT + BOIL_PT;
avgTemp = avgTemp / 2.0;
System.out.println("Halfway between is " + avgTemp + " degrees.");

}
}

pZombie
01-22-2012, 10:26
Create an instance of FreezeBoil and it will automagically print the header.

new FreezeBoil();


you would have to look into what the JVM calls first when creating an instance of a class.

I would have done it like this

class FreezyBoil {

FreezyBoil() {

whatever()

}

public void whatever() {...}



}


I wasn't aware you can do it the way you posted, but it really is not that complicated. You just have to know how java (the JVM) handles things when creating an instance of a class.

For example, the JVM will call the function which is equal to the name of the class, after declaring whatever variables you put in like in the following code:



class Test {



class Test {

Test() {
go();
}


public void go() {
System.out.println("hello world");
}


int i = 42;

{

System.out.println("the meaning of life is "+i);
}



public static void main(String[] args) {

new Test();
}
}



Note that it first works on int i = 42 and System.out.println.... before the JVM goes automatically to the Test() function (that is a JVM thing) which then calls the go() function manually.