Monday, March 9, 2015

Age Verifier

    I created a program to check if someone is over 18. I only used the main method just because this is a simple program and it went faster.

    The first 6 lines import the necessary files for creating an OptionPane and getting the date.

    From line 9 to 34 i am initializing variables for the current date and the entered date. Specifically, lines 27 to 29 are used to create the panes that get the users input.

    The rest of the code from line 35 on is checking the input with the current time to decide whether the person is 18. Depending on whether they are or aren't it will print in the console and in a pane if they are 18 or not.

    Currently there is nothing to handle if the person inputs a string, or the wrong format or if they click cancel. I am fairly new to JOptionPanes and don't know how to handle the cancel button. With the wrong input I could have handled it by using some if statements on the input.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JOptionPane;

public class Main {

 public static void main(String[] args) {

  String month, day, year;
  int imonth, iday, iyear, itmonth, itday, ityear;
  Date date = new Date();

  DateFormat monthf = new SimpleDateFormat("MM");
  DateFormat dayf = new SimpleDateFormat("dd");
  DateFormat yearf = new SimpleDateFormat("yyyy");

  String todaym = monthf.format(date);
  String todayd = dayf.format(date);
  String todayy = yearf.format(date);

  itmonth = Integer.parseInt(todaym);
  itday = Integer.parseInt(todayd);
  ityear = Integer.parseInt(todayy);

  month = JOptionPane.showInputDialog("Month of birth(MM)");
  day = JOptionPane.showInputDialog("Day of birth(DD)");
  year = JOptionPane.showInputDialog("Year of birth(YYYY)");

  imonth = Integer.parseInt(month);
  iday = Integer.parseInt(day);
  iyear = Integer.parseInt(year);

  if (iyear - ityear == -18) {
   if (imonth - itmonth == 0) {
    if (iday - itday <= 0) {
     System.out.println("you are 18");
     JOptionPane.showMessageDialog(null, "you are 18");
    } else if (iday - itday > 0) {
     System.out.println("you are not 18");
     JOptionPane.showMessageDialog(null, "you are not 18");
    }
   } else if (imonth - itmonth < 0) {
    System.out.println("you are 18");
    JOptionPane.showMessageDialog(null, "you are 18");
   } else if (imonth - itmonth > 0) {
    System.out.println("you are not 18");
    JOptionPane.showMessageDialog(null, "you are not 18");
   }
  } else if (iyear - ityear > -18) {
   System.out.println("you are not 18");
   JOptionPane.showMessageDialog(null, "you are not 18");
  } else if (iyear - ityear < -18) {
   System.out.println("you are 18");
   JOptionPane.showMessageDialog(null, "you are 18");
  }
 }
}

Wednesday, March 4, 2015

Head First Java Chapter 5

    In this chapter I learned about casting primitive variables, and using loops and I created a simple 1D game of battleship.

Casting Primitives:
  You can change one type of variable to another by casting it or using the parse command. You cast a variable by typing (TypeOfVariable). Sometimes this isn't possible like when going from a string to an integer so you can use Integer.parseInt().

Using Loops:
  When trying to do something over and over again you can use loops. There are multiple ways to use them. One is by doing this:
for (String name : nameArray) {}   This code is sets name to an element in the array each time
or
for (int i = 0; i < 20; i++) {} This code sets i to a number from 0 to 19  and then iterates that amount of times.

Battleship:
  This is the code that was used in this chapter to create a program like battleship that only uses a 1D array. I added some comments. 

GameHelper gets the users input:











SimpleDotCom contains the list of ships and the code for if you hit or not:



SimpleDotComTestDrive is the class with the main method, it utilizes the other two in order to run the whole thing:


Monday, February 9, 2015

Head First Java Chapter 4

    In this chapter I learned more about methods in a class. I learned about using private to hide variables so they don't get values I don't want them to have. If they are private you can still change and view them using getters and setters. I learned that all methods need a return type whether it be void or an actual type. I learned methods can have parameters so you can pass values into it.

Private:





Getters and Setters:







Return Type & Parameters:











Dog Code:

This is the code from chapter 4. I added comments explaining what everything does and had to move the test drive class into a new class file to run it in eclipse. This code uses getters and setters, private varibles, and return types. It makes 2 dogs and makes them bark. The type of bark depends on their size.



Thursday, February 5, 2015

Java Palindrome checker

I created a program the accepts a string and checks if it is a palindrome. It is not case sensitive but it will not work with sentences Unless you do not include the spaces between words. There are two classes the main chapter 3 class and the checker class. The checker class checks if it is a palindrome and if it is it prints a line saying that and if not it doesn't. the chapter 3 class get input and sends it to the checker class.


Chapter 3 Class:


Checker Class:



Thursday, January 22, 2015

Head First Java Chapter 3


    In this chapter I learned about primitive and reference variables. Primitive variables values are the bits representing the value. While a reference variable value is the bits representing a way to get to an object on the heap. A reference variable is null when not assigned an object. I learned about creating an array of variables and how it is always an object. I copied this code for a dog program. However, I moved the main method into its own class and put the dog class in a new file. I changed the second line to clarify what the dogs name on the last line was and fixed some grammar.

Dog Program:

Main File:

























Dog File:

















Output:

Thursday, January 15, 2015

Head First Java Chapter 2

    In this chapter I learned about the advantages of object oriented (OO) programing and what exactly it is. This chapter was sort of dull when it came to information about coding, but it had a lot of info on object oriented programs. Object oriented programing is different from procedural programing because with object oriented you think of the things in the program and in procedural you think of the things the program has to do. The main advantage OO has over procedural is that you don't have to edit existing code to add features or things. On the other hand procedural is usually faster to make. I also learned about how a class and a object can inherit methods and instance variables from a superclass or a class respectively.

Tuesday, January 13, 2015

Head First Java Chapter 1

    I read through chapter 1 and did the activities. In this chapter I learned the basics of java. I had no idea that in order to put an else with an if statement you must put it on the same line after the ending '}'.
Like This: 

if (x == 5) {
System.out.println("X = 5");
} else {
System.out.println("X != 5"):
}

    I also learned that empty space doesn't matter and every statement must end in a semi-colon. I leaened the basic structure of a java program which is class-method-statement.

x == 5 or x     ==     5
System.out.println("hi");

    I thought this was the mistake in the Beer program. I had to look back through the chapter to see this. I also learned the basic structure of a java program. The class is used to hold methods, which hold statements. You need a main method in order to start your program. 

    I fixed the Beer program by changing the if statement at the end and adding two if's inside the else. The problem was that one line printed "1 bottles of beer on the wall" which is not proper English. This was partially dealt with towards the beginning of the original program.

package javaapplication1;

public class BeerSong {

    public static void main(String[] args) {
        int beerNum = 99;
        String word = "bottles";
        
        while (beerNum > 0) {
            if (beerNum == 1) {
                word = "bottle"; //Changes bottles to singular if only 1.
            }
            
            System.out.println(beerNum + " " + word + " of beer on the wall");
            System.out.println(beerNum + " " + word + " of beer.");
            System.out.println("Take one down.");
            System.out.println("Pass it around.");
            beerNum = beerNum - 1;
            
            if (beerNum > 1) { //Had to change to 1 to not print '1 bottles of beer...'
                System.out.println(beerNum + " " + word + " of beer on the wall");
            } else {
                if (beerNum == 1) { //Added to make bottles singular when only 1.
                    System.out.println("1 bottle of beer on the wall");
                }
                if (beerNum == 0) { //Moved from original location to this if statement.
                    System.out.println("No more bottles of beer on the wall");
                }
            }
        }
    }

}