8 min read

Console

My First Java Program

I breezed through my first Java project with relative ease and little help. I can thank the year of struggling with things like Python and JavaScript for that level of comfort. I did need help with Operator Precedence and I probably will still need help with it in the interim. I also reached out to Stack Overflow for an answer: how do you escape and % sign in a format string? I knew \ plus a letter escapes a lot of line breaks and characters but it doesn’t escape a % sign in a format string. So how do you escape a % sign? With 2 % signs. Interesting.

I got to experience the thrill of debugging something myself. That feeling was satisfying. I am learning.

So what does my first Java program look like? Well, because of the requirements, the code is really verbose. I don’t think I could use a method call at all to sort of DRY it out. I may be able to do that down the road.

/* Author name: Tiffany White
This is a budget calculator
Created date: September 22, 2015
Last modified date: September 24, 2015
*/

// This line imports the Scanner class to the document
import java.util.Scanner;

public class P1A5_White
{
 public static void main(String[] args)
 {
    // This line declares a scanner variable that will be used to get user input
    Scanner keyboard = new Scanner(System.in);

    // This line prints text to the screen
    System.out.println("Welcome to Budget Calculator by Tiffany White. It will calculate your income and expenses.");

    // The lines like these print instructions to the screen and then get user input
    System.out.print("Enter your name.");
    String name = keyboard.nextLine();

    System.out.print("Enter your hourly rate.");
    double hourlyRate = keyboard.nextDouble();

    System.out.print("Enter your regular hours worked.");
    int regularHoursWorked = keyboard.nextInt();

    System.out.print("Enter your overtime hours worked.");
    double overtimeHoursWorked = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    System.out.print("Enter your rent.");
    double rent = keyboard.nextDouble();

    System.out.print("Enter your electric bill amount.");
    double elecBill = keyboard.nextDouble();

    System.out.print("Enter the amount of your water bill.");
    double waterBill = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    System.out.print("Enter the amount of your sewage bill.");
    double sewBill = keyboard.nextDouble();

    System.out.print("Enter the amount of your gas bill.");
    double gasBill = keyboard.nextDouble();

    System.out.print("Enter the amount of your food budget.");
    double foodBudget = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    System.out.print("Enter the amount of your entertainment expenses.");
    double entertainment = keyboard.nextDouble();

    System.out.print("Enter the amount of your car expenses.");
    double carExpenses = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    // This line calculates the user's gross pay
    double grossPay = (regularHoursWorked + overtimeHoursWorked) * hourlyRate;

    //This format string prints out the reults of the grossPay calculation and returns a new line
    System.out.printf("Your gross pay is " + "%.2f\%%, grossPay);

    // This line calculates expenses/deductions
    double deductions = (rent + elecBill + waterBill + sewBill + gasBill
    + foodBudget + entertainment + carExpenses);

    // This line declares variables in the double primitive data type
    double rentPercentage, elecBillPercentage, waterBillPercentage, sewBillPercentage,
    gasBillPercentage, foodBudgetPercentage, entertainmentPercentage, carExpensesPercentage;

    /* The following lines of code calculate the percentages of each deduction against
    the user's gross pay and prints out the results to the console, returning a new line
    */
    rentPercentage = (rent * 100) / grossPay;
System.out.printf("The amount of your gross pay that goes to rent is %.2f%%.\n", rentPercentage);

elecBillPercentage = (elecBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to your electric bill is %.2f%%.\n", elecBillPercentage);

    waterBillPercentage = (waterBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to your water bill is %.2f%%.\n", waterBillPercentage);

    sewBillPercentage = (sewBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to sewage is %.2f%%.\n", sewBillPercentage);

    gasBillPercentage = (gasBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to gas is %.2f%%.\n", gasBillPercentage);

    foodBudgetPercentage = (foodBudget * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to food is %.2f%%.\n", foodBudgetPercentage);

    entertainmentPercentage = (entertainment * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to entertainment is %.2f%%.\n", entertainmentPercentage);

    carExpensesPercentage = (carExpenses * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to your car is %.2f%%.\n", carExpensesPercentage);

    // This line calculates net pay after deductions are subtracted from the gross pay
    double netPay = (grossPay - deductions);

    // This format string prints out the net pay to the console and returns a new line
    System.out.printf("Your net pay after deductions is $" + "%.2f.\n", netPay);


  }



/* Author name: Tiffany White
This is a budget calculator
Created date: September 22, 2015
Last modified date: September 24, 2015
*/

// This line imports the Scanner class to the document
import java.util.Scanner;

public class P1A5_White
{
 public static void main(String[] args)
 {
    // This line declares a scanner variable that will be used to get user input
    Scanner keyboard = new Scanner(System.in);

    // This line prints text to the screen
    System.out.println("Welcome to Budget Calculator by Tiffany White. It will calculate your income and expenses.");

    // The lines like these print instructions to the screen and then get user input
    System.out.print("Enter your name.");
    String name = keyboard.nextLine();

    System.out.print("Enter your hourly rate.");
    double hourlyRate = keyboard.nextDouble();

    System.out.print("Enter your regular hours worked.");
    int regularHoursWorked = keyboard.nextInt();

    System.out.print("Enter your overtime hours worked.");
    double overtimeHoursWorked = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    System.out.print("Enter your rent.");
    double rent = keyboard.nextDouble();

    System.out.print("Enter your electric bill amount.");
    double elecBill = keyboard.nextDouble();

    System.out.print("Enter the amount of your water bill.");
    double waterBill = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    System.out.print("Enter the amount of your sewage bill.");
    double sewBill = keyboard.nextDouble();

    System.out.print("Enter the amount of your gas bill.");
    double gasBill = keyboard.nextDouble();

    System.out.print("Enter the amount of your food budget.");
    double foodBudget = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    System.out.print("Enter the amount of your entertainment expenses.");
    double entertainment = keyboard.nextDouble();

    System.out.print("Enter the amount of your car expenses.");
    double carExpenses = keyboard.nextDouble();

    // Clears keyboard buffer
    keyboard.nextLine();

    // This line calculates the user's gross pay
    double grossPay = (regularHoursWorked + overtimeHoursWorked) * hourlyRate;

    //This format string prints out the reults of the grossPay calculation and returns a new line
    System.out.printf("Your gross pay is " + "%.2f\%%, grossPay);

    // This line calculates expenses/deductions
    double deductions = (rent + elecBill + waterBill + sewBill + gasBill
    + foodBudget + entertainment + carExpenses);

    // This line declares variables in the double primitive data type
    double rentPercentage, elecBillPercentage, waterBillPercentage, sewBillPercentage,
    gasBillPercentage, foodBudgetPercentage, entertainmentPercentage, carExpensesPercentage;

    /* The following lines of code calculate the percentages of each deduction against
    the user's gross pay and prints out the results to the console, returning a new line
    */
    rentPercentage = (rent * 100) / grossPay;
System.out.printf("The amount of your gross pay that goes to rent is %.2f%%.\n", rentPercentage);

elecBillPercentage = (elecBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to your electric bill is %.2f%%.\n", elecBillPercentage);

    waterBillPercentage = (waterBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to your water bill is %.2f%%.\n", waterBillPercentage);

    sewBillPercentage = (sewBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to sewage is %.2f%%.\n", sewBillPercentage);

    gasBillPercentage = (gasBill * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to gas is %.2f%%.\n", gasBillPercentage);

    foodBudgetPercentage = (foodBudget * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to food is %.2f%%.\n", foodBudgetPercentage);

    entertainmentPercentage = (entertainment * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to entertainment is %.2f%%.\n", entertainmentPercentage);

    carExpensesPercentage = (carExpenses * 100) / grossPay;
    System.out.printf("The amount of your gross pay that goes to your car is %.2f%%.\n", carExpensesPercentage);

    // This line calculates net pay after deductions are subtracted from the gross pay
    double netPay = (grossPay - deductions);

    // This format string prints out the net pay to the console and returns a new line
    System.out.printf("Your net pay after deductions is $" + "%.2f.\n", netPay);


  }




}
See what I mean by verbose? But we’re beginners so I guess she wants to make sure we know what we’re doing. ¯_(ツ)_/¯

My Side Project

So I had the chance this week to work on my address book. I am looking for ways to use Google’s Contacts API and am in the process of looking for API courses or videos. I also want to write my own RESTful APIs eventually so I need to get cracking on the learning of that. I am only taking 6 credits so I have some in between time. It won’t be that way next fall as I try to get scholarships and with most scholarships, you have to be 12 credits to get them. It doesn’t bother me– mostly all my gen eds are completed so I can focus on Computer Science for the next two or three years. Anyway, I’ve used some simple Bootstrap to set up a few of the pages. Here’s the static html code for the index page:
<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>

<div class="navbar-header">

<ul class="nav nav-tabs">

<li role="presentation" class="active"><a href="index.html">Contacts</a></li>


<li role="presentation"><a href="addContacts.html">Add Contacts</a></li>


<li role="presentation"><a href="Phone.html">Phone</a></li>


<li role="presentation"><a href="Email.html">Email</a> </li>

      </ul>

  </div>


<div class="col-sm-3 col-md-3 pull-right">

<form class="navbar-form" role="search">

<div class="input-group">
              <input type="text" class="form-control" placeholder="Search" name="srch-term" id="srch-term">

<div class="input-group-btn">
                  <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
              </div>

          </div>

      </form>

  </div>


<div class="jumbotron">

<h1>My Contacts</h1>



Add your contacts to keep up with potential sales leads, anniversaries, birthdays, etc. You can add it all right here.

  </div>


</body>
<script type="text/javascript" src="main.js"></script>



<html lang="en">
<head>
    <meta charset="UTF-8">
    <!-- Latest compiled and minified CSS -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">

    <!-- Optional theme -->
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">

    <!-- Latest compiled and minified JavaScript -->
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="stylesheet.css">
</head>
<body>

<div class="navbar-header">

<ul class="nav nav-tabs">

<li role="presentation" class="active"><a href="index.html">Contacts</a></li>


<li role="presentation"><a href="addContacts.html">Add Contacts</a></li>


<li role="presentation"><a href="Phone.html">Phone</a></li>


<li role="presentation"><a href="Email.html">Email</a> </li>

      </ul>

  </div>


<div class="col-sm-3 col-md-3 pull-right">

<form class="navbar-form" role="search">

<div class="input-group">
              <input type="text" class="form-control" placeholder="Search" name="srch-term" id="srch-term">

<div class="input-group-btn">
                  <button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
              </div>

          </div>

      </form>

  </div>


<div class="jumbotron">

<h1>My Contacts</h1>



Add your contacts to keep up with potential sales leads, anniversaries, birthdays, etc. You can add it all right here.

  </div>


</body>
<script type="text/javascript" src="main.js"></script>




</html>
It’s going to be a Node app hosted on Heroku. I can’t wait to get it finished.