Author Topic: [Java] My first class. :D  (Read 6968 times)

0 Members and 1 Guest are viewing this topic.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
[Java] My first class. :D
« on: February 13, 2006, 01:37:17 pm »
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:
Code: [Select]
// 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

Code: [Select]
//*******************************************************************
// 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
« Last Edit: February 13, 2006, 01:38:57 pm by AntiVirus »
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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] My first class. :D
« Reply #1 on: February 13, 2006, 02:21:07 pm »
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. 

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: [Java] My first class. :D
« Reply #2 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
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

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] My first class. :D
« Reply #3 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'd personally do as Joe suggests

You might be right about that, Joe.


Offline MyndFyre

  • Boticulator Extraordinaire
  • x86
  • Hero Member
  • *****
  • Posts: 4540
  • The wait is over.
    • View Profile
    • JinxBot :: the evolution in boticulation
Re: [Java] My first class. :D
« Reply #4 on: February 13, 2006, 05:35:57 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.
I have a programming folder, and I have nothing of value there

Running with Code has a new home!

Our species really annoys me.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: [Java] My first class. :D
« Reply #5 on: February 13, 2006, 06:40:38 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

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] My first class. :D
« Reply #6 on: February 13, 2006, 07:03:10 pm »
Call it from what? A class.
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] My first class. :D
« Reply #7 on: February 13, 2006, 07:54:04 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. 

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:
Code: [Select]
System.out.println("Hello world!")is functional equivilant to this:
Code: [Select]
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. 


Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: [Java] My first class. :D
« Reply #8 on: February 13, 2006, 09:17:03 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)
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

Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] My first class. :D
« Reply #9 on: February 14, 2006, 12:07:01 am »
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)
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". 

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: [Java] My first class. :D
« Reply #10 on: February 14, 2006, 12:59:09 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)
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

Offline Joe

  • B&
  • Moderator
  • Hero Member
  • *****
  • Posts: 10319
  • In Soviet Russia, text read you!
    • View Profile
    • Github
Re: [Java] My first class. :D
« Reply #11 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.
Code: [Select]
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;
}
}

}
« Last Edit: February 14, 2006, 08:11:06 pm by Joe[e2] »
I'd personally do as Joe suggests

You might be right about that, Joe.


Offline iago

  • Leader
  • Administrator
  • Hero Member
  • *****
  • Posts: 17914
  • Fnord.
    • View Profile
    • SkullSecurity
Re: [Java] My first class. :D
« Reply #12 on: February 14, 2006, 08:38:16 pm »
"EasyReader"? 

Offline rabbit

  • x86
  • Hero Member
  • *****
  • Posts: 8092
  • I speak for the entire clan (except Joe)
    • View Profile
Re: [Java] My first class. :D
« Reply #13 on: February 14, 2006, 09:54:19 pm »
An Interface is a class structure, though, and isn't used on it's own.

Offline AntiVirus

  • Legendary
  • x86
  • Hero Member
  • *****
  • Posts: 2521
  • Best
    • View Profile
Re: [Java] My first class. :D
« Reply #14 on: February 14, 2006, 11:16:03 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.
Code: [Select]
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