User:SamFisher1022/Java

From Homestar Runner Wiki

Jump to: navigation, search

Hey juys. As you might have seen on my userpage, I'm learning Java in school. I tend to have obsessions when it comes to learning new stuff (this happened before when I was learning TI-BASIC), and I always want to learn at supersonic speed. I downloaded a nice Java IDE for home called JCreator, so now I can practice almost anytime. Below is sort of a journal of the Java source code I've written so far. If you have any hints or tips to make them better, give me a shout.

Contents

[edit] Class1

My first Java class compiled at home. All I could do, as you can see, was "println".

1  /**
2  * @(#)Class1.java
3  *
4  *
5  * @author 
6  * @version 1.00 2007/9/27
7  */
8 
9  public class Class1 
10 {
11     public static void main(String[] args)
12     {
13         System.out.println("Pikachu used Thundershock!");
14         System.out.println("A critical hit!");
15         System.out.println("It's super effective!");
16         System.out.println("The foe's Gyarados fainted! (surprisingly)");
17     }
18 }

[edit] StudentPoints

Learning the + operator was tough for us noobs. I fought with the compiler all class, and still couldn't get the code right. So I took the worksheets and sample code home and tried to compile it myself. With a little more thought, I got it to work (and was disappointed it's really so simple), then the problem became \t. I fought for another long while trying to get the columns to line up in the interpreter. Turns out spacing in the code does make a difference.

1 /**
2  * @(#)StudentPoints.java
3  *
4  *
5  * @author 
6  * @version 1.00 2007/10/1
7  */
8
9  public class StudentPoints
10 {
11     public static void main(String[] args)
12     	{
13     	// Prints a centered border and 1 blank line underneath
14     		System.out.println("\t <-------------------------------------->");
15     		System.out.println("\t | ==          Student Points        == |");
16     		System.out.println("\t <-------------------------------------->");
17    		System.out.println("");
18     	// Sets the table headings
19    		System.out.println("\tName      \t\tLab \t\tBonus \t\tTotal");
20    		System.out.println("\t----      \t\t--- \t\t----- \t\t-----");
21    	// Enter table data (Names and Scores)
22    		System.out.println("\tBob       \t\t34  \t\t20    \t\t" + (34 + 20));
23    		System.out.println("\tMary      \t\t37  \t\t22    \t\t" + (37 + 22));
24    		System.out.println("\tJoe       \t\t43  \t\t32    \t\t" + (43 + 32));
25    		System.out.println("\tGary      \t\t29  \t\t32    \t\t" + (29 + 32));
26    		System.out.println("\tJohn Kurt \t\t100 \t\t100   \t\t" + (100 + 100));
27     	}
28 }

[edit] ThingThing

This one's sort of odd. It's based on a TI game I'm writing called "Textventure." We were introduced to basic data types today, and I read ahead in the textbook to find operators for conditional statements just to get this started. As you can probably tell, it's very "just started." Right now, the code is broken. Line 18 seems to be the trouble, and the compiler's errors are "illegal start of type" and "<identifier> expected." "Illegal start of type" error also exists in line 23. As a noob, I don't know what this means or how to fix it. If you do, please help me out here.

1  // ****************************************************************************************************************
2  // ThingThing.java
3  // 
4  // Purpose == false
5  // ****************************************************************************************************************
6
7  public class ThingThing
8  {
9 	// Sets up variables: Str1, room
10 	// "|variable|" in comments refers to variables
11 	public static void main (String[] args)
12	{
13		String Str1 = "<Str1 is empty>";
14		int room = 22;
15	}
16	
17	// Displays room descriptions based on value of |room|
18	if (room == 22)
19	{
20		System.out.println("You are standing in a very plain field.");
21		System.out.println("There is a lone tree and lots of... nothing else.");
22      }
23	else
24	{
25		System.out.println("What have you done!?");
26		System.out.println("YOU'VE BLOWN UP ROOM 22!!");
27	}
28 }

[edit] BaseConverter

This is my first halfway practical program. It converts any positive integer to a user-selected base; for example, it can convert numbers to binary. One sad thing about it is that I don't know how to get it to print the converted digits from right to left. As a result, the converted number prints backwards. Another problem with the program is that it can't "check" the user's inputs to make sure they are valid requests. A "Go to line #" command or something would be a great help. After that, I'll likely try to make it able to convert up to hexadecimal. Anyway, here's the source code:

1  // ******************************************************
2  // BaseConverter.java
3  // 
4  // Converts base 10 numbers to any base between 2 and 10
5  // ******************************************************
6 
7  import java.util.Scanner;
8  
9  public class BaseConverter
10 {
11 	public static void main (String[] args)
12 	{
13 		// Initial setup; welcomes the user and creates variables |num| and |base|
14 		Scanner scan = new Scanner(System.in);
15 		System.out.println("Welcome to the base converter!");
16 		int num = 0, base = 10;
17 		
18 		// Prompts user input of |num| and |base|
19 		System.out.println("Please enter any number in base 10.");
20 		num = scan.nextInt();
21 		System.out.println("Please enter the base you want to convert it to.");
22 		System.out.println("Your base must be between 2 and 10.");
23 		base = scan.nextInt();
24 		int NUM = num;
25 		
26 		// Converts int |num| to base |base|
27 		while (num != 0)
28 		{
29 			System.out.print(num % base);
30 			num = num / base;
31 		}
32 		
33 		// Explains the converted number
34 		System.out.println(" is " + NUM + " in base " + base + ".");
35 		System.out.println("Backwards.");
36 	}
37 }
Personal tools