On My First 100% on a Java Program

Got a 100/100 on my first Intermediate Java Program.

I have been working really hard at being consistent in my life: consistent bedtimes, consistent night time medication times, consistent hygiene times (you’d be surprised at how weird my hygiene patterns were when I had that breakdown this time last year), consistent eating, notes & homework…you get the gist.

So far I have been doing just that, with the few night time medication hiccups of late, but for the most part, everything is consistent.

I have been combing over my class notes and making consolidating them in Evernote for reference later. I have been programming and completing assignments well before they are due.

This brings me to my next point.

Confidence

Now that I have programming experience under my belt with Free Code Camp and Treehouse, I am experiencing a bit more confidence this semester, the confidence I lacked last time I took CS 0401.

I can do the whole, Read-Search-Ask method of finding how to solve something given by Free Code Camp. This has been instrumental in getting as far as I have. This method of learning how to program is the best way, I believe, instead of being handheld through the process.

I understand why something works and why it doesn’t. And our professor, Tim Hoffman, explains so much stuff so clearly I can actually do the assignments without much stress.

For instance:


// F16 CS 401 Speeding Ticket Project
// Project1.java Starter File

import java.io.*;
import java.util.*;

public class Project1
{
  public static void main (String args[])
{
  // Create a scanner to read from keyboard
  Scanner kbd = new Scanner (System.in);

  String firstName="N/A",lastName="N/A";
  int age=0, speedLimit=0, actualSpeed=0, mphOverLimit=0;
  int baseFine=0, underAgeFine=0, zoneFine=0, totalFine=0;
  // DO NOT ADD TO OR MODIFY ABOVE THIS LINE

  // your variables & code in here

  System.out.println("Enter your first and last name. \> ");
  firstName = kbd.next();
  lastName = kbd.next();

  // DO NOT ADD TO OR MODIFY BELOW THIS LINE
  System.out.println();
  System.out.format( "name: %s, %s \n",lastName,firstName );
  System.out.format( "age: %d yrs.\n",age );
  System.out.format( "actual speed: %d mph.\n",actualSpeed );
  System.out.format( "speed limit: %d mph.\n",speedLimit );
  System.out.format( "mph over limit: %d mph.\n",mphOverLimit );
  System.out.format( "base fine: $%d\n",baseFine );
  System.out.format( "zone fine: $%d\n",zoneFine );
  System.out.format( "under age fine: $%d\n",underAgeFine );
  System.out.format( "total fine: $%d\n",totalFine );
  } // END MAIN
} // END PROJECT1 CLASS

When asking for the first and last names in one string, I knew instinctively, because of Tim, that the characters in strings are tokenized in the string buffer. Once it gets to a space in the input, it stops tokenizing, and the next string is held in the buffer. If you were to ask for an nextInt() for the following input, you’d get an error. To avoid this, you usually do firstName = kbd.nextLine(). But since I needed to essentially split these so that they would look like:

Hoffman, Tim

on one line, I could keep each separate part of the string input in the string buffer using firstName = kbd.next(); lastName = kbd.next() and it would spit out the correct output. Pretty cool. I wouldn’t have known about this if not for Tim. I learned it on 0007 but I didn’t quite understand.

Aced the First Project

Got a 100% on Project 1. It helps he fills out the output that he expects but he also lets you do it as many times as you can until a certain point after the project/lab is due. This is the first time I’ve gotten 100% on any difficult programming assignment since I’ve been going to Pitt. Yes, it’s a text based Speeding Ticket Calculator that basically relies on Scanner input and conditionals and some simple math, but I hadn’t even gotten 100% on my Beginning Programming projects, let alone the Intermediate programming projects. This boosts confidence and gives me hope for the future.

First Java Challenge and Getting to Know Variable Scope

\"Head_desk\"

So I have stumbled a bit in the intro class, but not too badly. When working with opening text files, writing text to those text files, and then reading the text back into the program, I got stuck with scope. And I knew it. It wasn’t like I was flying blind without knowing my problem which, after a year of doing this, I am pretty proud of. I still couldn’t figure out where my issue was. I went to Stack Overflow, I asked around. For one, this is what my code looked like prior to getting it corrected:

import java.util.Scanner;
import java.io.*;

public class White
{
 public static void main(String[] args) throws IOException
 {
   // Gets the PrintWriter and Scanner classes
   PrintWriter file = new PrintWriter("names.txt");
   Scanner keyboard = new Scanner(System.in);
   // Gets data and writes it to a file
  for (int i = 1; i <= 3; i++)
 {
    //Gets name of friend
    System.out.print("Enter the name of friend number " + i + ": ");
    String friendName = keyboard.nextLine();
    file.println(friendName);
 }

    FileWriter fw = new FileWriter("names.txt", true);
    PrintWriter pw = new PrintWriter(fw);
    for (int i = 4; i <= 6; i++)
    {
        //Gets name of friend
        System.out.print("Enter the name of friend number " + i + ": ");
        String friendName = keyboard.nextLine();
        file.println(friendName);

    }

    file.close();
  }

}

As you can see I was trying to access a variable outside it’s scope

String friendName = keyboard.nextLine();

. I declared it twice as well. I knew I needed to change the variable to be outside of all the loops, global (but not quite, as I was told about the resulting code, not sure why) sort of. I would declare it right after the method but the compiler complained that I did not initialize

String friendName;

. And I didn’t. I didn’t know how.

So frustrated, I jumped on the Code Newbie Slack general channel and just threw it out there. Most are web developers but someone helped. Cory. He told me to initialize it with empty single quotes. The compiler didn’t understand the empty single quotes so I changed them to an empty string with double quotes. Here is the final program, which isn’t elegant, but I still got a 100 anyway, as it was just for participation:

import java.util.Scanner;
import java.io.*;

public class White
{
 public static void main(String[] args) throws IOException
 {
   // Gets the PrintWriter and Scanner classes
   PrintWriter file = new PrintWriter("names.txt");
   Scanner keyboard = new Scanner(System.in);
   String friendName = "";
   // Gets data and writes it to a file
   for (int i = 1; i <= 3; i++)
 {
     //Gets name of friend
     System.out.print("Enter the name of friend number " + i + ": ");
     friendName = keyboard.nextLine();
     file.println(friendName);
     System.out.println(friendName);
 }

    FileWriter fw = new FileWriter("names.txt", true);
    PrintWriter pw = new PrintWriter(fw);
    for (int i = 4; i <= 6; i++)
    {
        //Gets name of friend
        System.out.print("Enter the name of friend number " + i + ": ");
        friendName = keyboard.nextLine();
        file.println(friendName);
        System.out.println(friendName);

    }

    file.close();
  }

}

Fin. Whew.