Wednesday, September 28, 2011

First Run-In With Methods

Purpose of this program: Create a multiplication flashcard generator for a young relative, and have it repeat 10 times.


package lab3;
//Importing Random Class
import java.util.Random;
import java.util.Scanner;

public class Rand
{
 public static void main(String[] args)
 {
  flashcards();
  flashcards();
  flashcards();
  flashcards();
  flashcards();
  flashcards();
  flashcards();
  flashcards();
  flashcards();
  flashcards();
 }

 public static void flashcards()
 {
  // Inputs

  Scanner in = new Scanner(System.in);
  Random rand = new Random();
  int randomNumber = rand.nextInt(11);

  int randomNumber2 = rand.nextInt(11);
  // Outputs
  System.out.print("What is " + (randomNumber) + " x " + (randomNumber2)
    + "? ");

  // Inputs
  int answer = in.nextInt();
  String answerString = "";
  answerString = Integer.toString(answer);

  System.out.println("Your answer: " + answer);
  System.out.println("Correct answer: "
    + String.valueOf(randomNumber * randomNumber2));

  String randomEquation = String.valueOf(randomNumber * randomNumber2);
  // Outputs
  if (answerString.equals(randomEquation))
  {
   System.out.println("Correct!");

  }
  if (!answerString.equals(randomEquation))
  {
   System.out.println("Incorrect.");
  }

 }
}

Wednesday, September 14, 2011

Chapter 1 Practice Code

Here are some generic codes, along with what they print:


System.out.println("\"Quotes\"");
System.out.println("Slashes \\//");
System.out.println("How '\"confounding' \"\\\" it is!");

System.out.println("name\tage\theight");
System.out.println("Archie\t17\t5'9\"");
System.out.println("Betty\t17\t5'6\"");
System.out.println("Jughead\t16\t6'");

System.out.println("Shaq is 7'1");
System.out.println("The string \"\" is an empty message.");
System.out.println("\\'\"\"");

Here is the output generated:

"Quotes"
Slashes \//
How '"confounding' "\" it is!
name age height
Archie 17 5'9"
Betty 17 5'6"
Jughead 16 6'
Shaq is 7'1
The string "" is an empty message.
\'""

This may seem like a lot of useless tidbits, but the main point was to decide which characters do what in java. We learned /t is some kind of table separating function (mind you I'm doing these without any prior knowledge so be gentle if I'm way off. The rest seemed pretty straightforward.

This little post will help you solve the problems:
confounding, Archie, and Shaq on the site for Building Java Programs: Practice-It!

Wednesday, April 6, 2011

Still Sidetracked

Just can't get going on this stuff. I'm trying my best though. Stay tuned.

blogger templates | Make Money Online