News:

Wieners, Brats, Franks, we've got 'em all.

Main Menu

[Java] My first class. :D

Started by AntiVirus, February 13, 2006, 01:37:17 PM

Previous topic - Next topic

0 Members and 3 Guests are viewing this topic.

AntiVirus

All this program does is divide/add/subtract/multiply the number it gets from the user by 18 and then prints the value on the screen. 

Class:

// Mine.java       By: Brandon
// A class that adds/multiplies/divides/subtracts 18
//********************************************************************

public class Mine
{
public double answer;

//---------------------------------------------------------------
// Multiples the number by 18 and then returns the new value
//---------------------------------------------------------------
public double multi (double num1)
{
answer = num1 * 18;
return (answer);
}

//---------------------------------------------------------------
// Adds the number with 18
//---------------------------------------------------------------
public double add (double num1)
{
answer = num1 + 18;
return (answer);
}

//---------------------------------------------------------------
// Subtracts 18
//---------------------------------------------------------------
public double sub (double num1)
{
answer = num1 - 18;
return (answer);
}
//---------------------------------------------------------------
// Divides the number by 18
//---------------------------------------------------------------
public double divide (double num1)
{
answer = num1 / 18;
return (answer);
}
}


And then here is the program that utilizes that class


//*******************************************************************
// MineAgain.java          By: Brandon
// Using the Mine class.
//*******************************************************************

import cs1.Keyboard;
public class MineAgain

{
//________________________________________________________________
// Should do an multiple/subtract/divide/add everything by 18.
//________________________________________________________________
public static void main (String[] args)
{
String again = "y";

while (again.equalsIgnoreCase("y"))
{

double num1;
double answer;
int todo;

Mine myAgain = new Mine();

System.out.println ("Add(1), Multiply(2), Subtract(3), Divide(4): ");
todo = Keyboard.readInt();

if (todo ==2)
{
System.out.println ("Number: ");
num1 = Keyboard.readDouble();
answer = myAgain.multi(num1); // Multiplies the number by 18
System.out.println ("The answer is: " + answer);
}

if (todo ==1)
{
System.out.println ("Number: ");
num1 = Keyboard.readDouble();
answer = myAgain.add(num1); // Adds the number with 18
System.out.println ("The answer is: " + answer);
}
if (todo == 3)
{
System.out.println ("Number: ");
num1 = Keyboard.readDouble();
answer = myAgain.sub(num1); // Subtracts the number by 18
System.out.println ("The answer is: " + answer);
}
if (todo == 4)

{
System.out.println ("Number: ");
num1 = Keyboard.readDouble();
answer = myAgain.divide(num1); // Divides the number by 18
System.out.println ("The answer is: " + answer);
}

System.out.println ("Again? (y/n): "); // While loop.
again = Keyboard.readString();
}
}
}

It's pretty much a worthless program, but I was just proud I made my first class. :D

Ooh, and any types on how to make this better would be much appreciated. :D
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

iago

Isn't your first class supposed to be "hello world"?  You can't break tradition!

The first thing I notice is that your indenting is messed up, although that could be the forum software. 

AntiVirus

Rofl, no that's just how I idented it. :)

And, "Hello World" = the shitty. :D  There isn't really any need to create a class for hello word.. Just use the println method. :D
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Joe

print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


MyndFyre

Quote from: Joe[e2] on February 13, 2006, 05:20:35 PM
print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)

That's great that you feel more controlful, but printing an endline with println is better.
Quote from: Joe on January 23, 2011, 11:47:54 PM
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Quote from: Rule on May 26, 2009, 02:02:12 PMOur species really annoys me.

AntiVirus

Quote from: Joe[e2] on February 13, 2006, 05:20:35 PM
print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)
I don't need to make a class. It was already made, I just need to call it. :D
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Joe

Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


iago

Quote from: AntiVirus on February 13, 2006, 04:51:38 PM
Rofl, no that's just how I idented it. :)

And, "Hello World" = the shitty. :D  There isn't really any need to create a class for hello word.. Just use the println method. :D
Everything you write in Java is within a class.  Notice that even a "hello world" program starts with "public class _____"?  That's because it's in a class. 

Quote from: Joe[e2] on February 13, 2006, 05:20:35 PM
print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)
Yes, you have more control when you use it, but it's also potentially problemmatic. 

This:
System.out.println("Hello world!")
is functional equivilant to this:
System.out.println("Hello world!" + System.getProperty("line.separator"));
System.out.flush();


Since I'm sure you don't do that, you have a portability issue on platforms that don't automatically flush complete lines or platforms that don't use the same end-of-line character as yours. 


AntiVirus

Quote from: Joe[e2] on February 13, 2006, 05:20:35 PM
print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)
Quote from: Joe[e2] on February 13, 2006, 07:03:10 PM
Call it from what? A class.
Yes, call it from a class that was already written.  You said I need to make it, when that's not true.  The class that has the method pint(ln) has already been made, I just need to call it to use it in my program.
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

iago

Quote from: AntiVirus on February 13, 2006, 09:17:03 PM
Quote from: Joe[e2] on February 13, 2006, 05:20:35 PM
print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)
Quote from: Joe[e2] on February 13, 2006, 07:03:10 PM
Call it from what? A class.
Yes, call it from a class that was already written.  You said I need to make it, when that's not true.  The class that has the method pint(ln) has already been made, I just need to call it to use it in my program.

No, you call it FROM your own class. 

It looks like:

public class Example
{
    public static void main(String []args)
    {
        System.out.println("Hi there, world!");
    }
}

There are 3 classes being used there:
- Example --> your class
- String and System --> Java's classes

So your class is "Example". 

AntiVirus

Quote from: iago on February 14, 2006, 12:07:01 AM
Quote from: AntiVirus on February 13, 2006, 09:17:03 PM
Quote from: Joe[e2] on February 13, 2006, 05:20:35 PM
print > println. I feel more controlful when I use it!

On a side note, you need to make a class to do anything, including calling print(ln)
Quote from: Joe[e2] on February 13, 2006, 07:03:10 PM
Call it from what? A class.
Yes, call it from a class that was already written.  You said I need to make it, when that's not true.  The class that has the method pint(ln) has already been made, I just need to call it to use it in my program.

No, you call it FROM your own class. 

It looks like:

public class Example
{
    public static void main(String []args)
    {
        System.out.println("Hi there, world!");
    }
}

There are 3 classes being used there:
- Example --> your class
- String and System --> Java's classes

So your class is "Example". 
So... I call the println method from my class?
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus

Joe

#11
Si senior.

EDIT -
Two posts ago, I think it was kind of obvious that absolutely everything in Java is a class. Well, I didn't mean to say that. There's also interfaces, and something else that I can't remember at the moment (or maybe not?).

EDIT -
This is outright pimp. (C) 2006 Joetheodd.
public class ChuckNorris
{

EasyReader in = new EasyReader();


public static void main(String args[])
{
new ChuckNorris();
}


public ChuckNorris()
{
boolean answered = false;
while(true)
{
System.out.println("Enter your question for Chuck Norris.");
System.out.print("Q: ");
String q = in.readLine();

if (q.equalsIgnoreCase("Is your real name Charles?"))
{
System.out.println("A: Never question Chuck Norris.");
answered = true;
}

if (q.equalsIgnoreCase("Have you ever counted to infinity?"))
{
System.out.println("A: Yes. Twice.");
answered = true;
}

if (q.equalsIgnoreCase("I don't have one."))
{
System.exit(0);
answered = true;
}

if (!answered)
{
System.out.println("A: That question was not understood. Chuck Norris should roundhouse kick you, but you're not worth it.");
}
answered = false;
}
}

}
Quote from: Camel on June 09, 2009, 04:12:23 PMI'd personally do as Joe suggests

Quote from: AntiVirus on October 19, 2010, 02:36:52 PM
You might be right about that, Joe.


iago


rabbit

An Interface is a class structure, though, and isn't used on it's own.

AntiVirus

Quote from: Joe[e2] on February 14, 2006, 07:32:36 PM
Si senior.

EDIT -
Two posts ago, I think it was kind of obvious that absolutely everything in Java is a class. Well, I didn't mean to say that. There's also interfaces, and something else that I can't remember at the moment (or maybe not?).

EDIT -
This is outright pimp. (C) 2006 Joetheodd.
public class ChuckNorris
{

EasyReader in = new EasyReader();


public static void main(String args[])
{
new ChuckNorris();
}


public ChuckNorris()
{
boolean answered = false;
while(true)
{
System.out.println("Enter your question for Chuck Norris.");
System.out.print("Q: ");
String q = in.readLine();

if (q.equalsIgnoreCase("Is your real name Charles?"))
{
System.out.println("A: Never question Chuck Norris.");
answered = true;
}

if (q.equalsIgnoreCase("Have you ever counted to infinity?"))
{
System.out.println("A: Yes. Twice.");
answered = true;
}

if (q.equalsIgnoreCase("I don't have one."))
{
System.exit(0);
answered = true;
}

if (!answered)
{
System.out.println("A: That question was not understood. Chuck Norris should roundhouse kick you, but you're not worth it.");
}
answered = false;
}
}

}

Lol, I think that is even more worthless than mine. :)
The once grove of splendor,
Aforetime crowned by lilac and lily,
Lay now forevermore slender;
And all winds that liven
Silhouette a lone existence;
A leafless oak grasping at eternity.


"They say that I must learn to kill before I can feel safe, but I rather kill myself then turn into their slave."
- The Rasmus