20120211

Portfolio

MATLAB Graphical User Interface (GUI): Projectile Motion

MATLAB is yet another application and programming language I've added to my skill set this year. In this post I'm going to share with you a program I wrote in MATLAB that models projectile motion. It has a convenient little Graphical User Interface (GUI) where the user can input the initial velocity (m/s) and angle (degrees),  and the program will spit out a x-y plot. 

[Download This!]

20120201

Java Project #12: Statistics


/**
 * class Statistics finds the mean and standard deviation of given numbers.
 * 
 * @author (Andrew Samuels) 
 * @version (04-09-2010)
 */
//import scanner
import java.util.Scanner;

public class Statistics
{
    
    public static void main(String[] args)
    {
        //create variables
        int size;
        int i;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture size from user
        System.out.print("How many numbers will be entered: ");
        size = keyboard.nextInt();
        
        //create array of "size"
        double[] p = new double[size];
        
        //read numbers from user one by one
        for(i = 0; i < size; i ++)
        {
            System.out.print("Enter number " + (i + 1) +": ");
            p[i] = keyboard.nextDouble();
        }
        
        //find mean
        double sum = 0;
        for(i = 0; i < size; i++)
        {
            sum = sum + p[i];
        }
        
        double mean = sum/(double)size;
        System.out.println("Mean: " + mean);
        
        //find standard deviation
        double temp = 0;
        for (i = 0; i < size; i++)
        {
            temp = temp + (p[i] - mean)*(p[i] - mean);
        }
        temp = Math.sqrt(temp/size);
        
        System.out.println("Standard deviation: "+ temp);
    }
}

Java Project #11: Array Work

This script shows off various array operations.


/**
 * class ArrayWork.
 * 
 * @author (Andrew Samuels) 
 * @version (04-05-2010)
 */
//import scanner
import java.util.Scanner;

public class ArrayWork
{
    
    public static void main(String[] args)
    {
        //create variables
        int length;
        int i;
        int zeros;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture length from user
        System.out.print("Enter the length of array: ");
        length = keyboard.nextInt();
        
        int[] p = new int[length]; //creates array of length, "length"
        System.out.println();
        
        //read integers one by one
        for(i = 0; i < length; i ++)
        {
            System.out.print("Enter number " + (i + 1) +": ");
            p[i] = keyboard.nextInt();
        }
        
        //print numbers in order user entered them
        System.out.print("Numbers: ");
        System.out.println();
        for(i = 0; i < length; i++)
        {
            System.out.print(p[i] + " ");
        }
        
        
        //find sum and average of of numbers and print them
        int sum = 0;
        for(i = 0; i < length; i++)
        {
            sum = sum + p[i];
        }
        System.out.println();
        System.out.println("Sum: " + sum);
        System.out.println();
        
        double average = sum/(double)length;
        System.out.println("Average: " + average);
        System.out.println();
        
        //find max and min in array and print
        int maximum = p[0];
        for (i = 0; i < length; i++)
        {
            if (p[i] > maximum)
            {
                maximum = p[i];
            }
        }
        System.out.println("Maximum: " + maximum);
        System.out.println();
        
        int minimum = p[0];
        for (i = 0; i < length; i++)
        {
            if (p[i] < minimum)
            {
                minimum = p[i];
            }
        }
        System.out.println("Minimum: " + minimum);
        System.out.println();
        
        //print all positive numbers
        
        System.out.println("Positives: ");
        for (i = 0; i < length; i++)
        {
            if (p[i] > 0)
            {
                System.out.print(p[i]+" ");
            }
        }
        
        //find and print zeros
        zeros = countZeros(p);
        System.out.println("Zeros: " + zeros);
        
        //reverse array and print
        printReverse(p);
        
        
    }
    
    //count zeros method
    private static int countZeros(int[] array)
    {
        int answer = 0;
        int i;
        for(i = 0; i < array.length; i++)
        {
            if(array[i] == 0)
            {
                answer = answer + 1;
            }
         
        }
        return answer;
    }
    
    //print reverse method
    private static void printReverse(int[] array)
    {
        int i;
        for(i = array.length - 1; i >= 0; i--)
        {
            System.out.print(array[i] + " ");
        }
        
    }
   
    //display method
    public static void display(int[] array)
    {
        for(int i = 0; i < array.length; i++)
        {
            System.out.println(array[i]);
        }
    }
}

Java Project #10: Area Finder


/**
 * class AreaFinder compute area of square, rectangle, circle.
 * 
 * @author (Andrew Samuels) 
 * @version (03-29-2010)
 */
public class AreaFinder
{
    private static final double pi = 3.1415; //value of constant pi
    private static int count = 0; //count of times area computed
    
    //computes area of square
    public static double squareArea(double sideOfSquare)
    {
        double areaOfSquare = sideOfSquare * sideOfSquare;
        count++;
        return areaOfSquare;
        
    }
    
    //computes area of rectangle
    public static double rectangleArea(double width, double height)
    {
        double areaOfRectangle = width * height;
        count++;
        return areaOfRectangle;
        
    }
    
    //computes area of circle
    public static double circleArea(double radius)
    {
        double areaOfCircle = pi * radius * radius;
        count++;
        return areaOfCircle;
        
    }
    
    //returns number of times the area computing methods were called
    public static int getCallCount()
    {
        return count;
    }
}

20120129

Java Project #9: Pay Raise


/**
 * PayRaise computes pay raise and new salary for employees.
 * 
 * @author (Andrew Samuels) 
 * @version (02-19-2010)
 */

//import scanner
import java.util.Scanner;

public class PayRaise
{

    public static void main(String[] args)
    {
        //create variables
        double currentSalary;
        double payRaise;
        double newSalary;
        String word;
        char performanceRating;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture current salary from user
        System.out.print("Please enter current salary: ");
        currentSalary = keyboard.nextDouble();
        
        //capture performance rating from user
        System.out.print("Please enter performance rating (A, B, C): ");
        word = keyboard.next();
        performanceRating = word.charAt(0);
        
        //determine pay raise using switch/case
        switch(performanceRating)
        {
            case 'A':
                payRaise = currentSalary * .06;
                newSalary = currentSalary + payRaise;
                System.out.println("Pay raise is: " + payRaise);
                System.out.println("New salary is: " + newSalary);
                break;
            case 'B':
                payRaise = currentSalary * .04;
                newSalary = currentSalary + payRaise;
                System.out.println("Pay raise is: " + payRaise);
                System.out.println("New salary is: " + newSalary);
                break;
            case 'C':
                payRaise = currentSalary * .02;
                newSalary = currentSalary + payRaise;
                System.out.println("Pay raise is: " + payRaise);
                System.out.println("New salary is: " + newSalary);
                break;
            default:
                System.out.println("Invalid performance rating");
        }
    }
}

Java Project #8 : Tax Program


/**
 * TaxProgram compute tax according to marital status and income.
 * 
 * @author (Andrew Samuels) 
 * @version (02-19-2010)
 */

//import scanner
import java.util.Scanner;

public class TaxProgram
{

    public static void main(String[] args)
    {
        //create variables
        int income;
        int taxAmount;
        String maritalStatus;
        
        //create scanner
        Scanner keyboard =  new Scanner(System.in);
        
        //capture income from user
        System.out.print("Please enter income: ");
        income = keyboard.nextInt();
        
        //capture marital status from user
        System.out.print("Please enter marital status (single, married): ");
        maritalStatus = keyboard.next();
        
        //determine tax amount from marital status and income
        if ((maritalStatus.equals("single") || maritalStatus.equals("Single")) && income < 30000)
        {
            taxAmount = income * 1/5;
            System.out.println("Tax amount is: " + taxAmount);
        }
        else if ((maritalStatus.equals("single") || maritalStatus.equals("Single")) && income > 30000)
        {
            taxAmount = income * 1/4;
            System.out.println("Tax amount is: " + taxAmount);
        }
        else if ((maritalStatus.equals("married") || maritalStatus.equals("Married")) && income < 50000)
        {
            taxAmount = income * 1/10;
            System.out.println("Tax amount is: " + taxAmount);
        }
        else if ((maritalStatus.equals("married") || maritalStatus.equals("Married")) && income > 50000)
        {
            taxAmount = income * 3/20;
            System.out.println("Tax amount is: " + taxAmount);
        }
    }
}

Java Project #7 : Student Grades (Upgraded!)


/**
 * StudentGrades computes the grades of students in a class.
 * 
 * @author (Andrew Samuels) 
 * @version (02-18-2010)
 */

//import scanner
import java.util.Scanner;

public class StudentGrades
{
    
    public static void main(String[] args)
    {
        //create variables
        int homeworkScore;
        int midtermScore;
        int finalexamScore;
        double finalcourseScore;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture total homework score from user
        System.out.print("Please enter total homework score (0-200): ");
        homeworkScore = keyboard.nextInt();
        
        //capture midterm score from user
        System.out.print("Please enter midterm score (0-100): ");
        midtermScore = keyboard.nextInt();
        
        //capture final exam score from user
        System.out.print("Please enter final exam score (0-100): ");
        finalexamScore = keyboard.nextInt();
        
        //determine final course score
        finalcourseScore = (50*(homeworkScore/200.0) + 20*(midtermScore/100.0) + 30*(finalexamScore/100.0));
        
        //print final course score
        System.out.println("Final course score is: " + finalcourseScore);
        
        //determine letter grade and meaning of the grade
        if (finalcourseScore >= 90 && finalcourseScore <= 100)
        {
            System.out.println("Letter grade is: A");
            System.out.println("You are: Distinctly above average");
        } 
        else if (finalcourseScore >= 80 && finalcourseScore < 90)
        {
            System.out.println("Letter grade is: B");
            System.out.println("You are: Above average");
        }
        else if (finalcourseScore >= 70 && finalcourseScore < 80)
        {
            System.out.println("Letter grade is: C");
            System.out.println("You are: Average");
        }
        else if (finalcourseScore >= 60 && finalcourseScore < 70)
        {
            System.out.println("Letter grade is: D");
            System.out.println("You are: Below average");
        }
        else if (finalcourseScore >= 0 && finalcourseScore < 60)
        {
            System.out.println("Letter grade is: E");
            System.out.println("You are: Failed");
        }
    }
}

Java Project #6 : Points Distance


/**
 * PointsDistance computes the distance between two points in the two dimensional plane.
 * 
 * @author (Andrew Samuels) 
 * @version (02-04-2010)
 */

//import Scanner from library
import java.util.Scanner;

public class PointsDistance
{
    public static void main(String[] args)
    {
        //create distance formula variables
        double x1;
        double y1;
        double x2;
        double y2;
        double distanceBetweenPoints;
        
        //Create Scanner
        Scanner keyboard = new Scanner(System.in);
        
        //capture x1 from user
        System.out.print("Please enter x coordinate of first point: ");
        x1 = keyboard.nextDouble();
        
        //capture y1 from user
        System.out.print("Please enter y coordinate of first point: ");
        y1 = keyboard.nextDouble();
        
        //capture x2 from user
        System.out.print("Please enter x coordinate of second point: ");
        x2 = keyboard.nextDouble();
        
        //capture y2 from user
        System.out.print("Please enter y coordinate of second point: ");
        y2 = keyboard.nextDouble();
        
        
        //calculate distance between points using distance forumula
        distanceBetweenPoints = Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
        
        
        //print distance between the points
        System.out.println("Distance between (" + x1 + ", " + y1 + ") and (" + x2 + ", " + y2 +") is: " + distanceBetweenPoints);
    }
}

Java Project #5 : Bank Investment


/**
 * BankInvestment computes the total money earned in an investment bank.
 * 
 * @author (Andrew Samuels) 
 * @version (02-04-2010)
 */
//importing Scanner from library
import java.util.Scanner;

public class BankInvestment
{
    public static void main(String[] args)
    {
    //create variables
    int initialInvestmentMoney; //initial investment money in dollars
    double interestRate; //interest rate in percentage
    int numberOfYears; //number of years of investment
    double totalMoneyEarnedDouble; //total money earned, double type, (converted to integer later)
    
    //Create Scanner
    Scanner keyboard = new Scanner(System.in);
        
    //capture initial investment from user
    System.out.print("Please enter initial investment (dollars): ");
    initialInvestmentMoney = keyboard.nextInt();
    
    //capture interest rate from user
    System.out.print("Please enter interest rate (percent): ");
    interestRate = keyboard.nextDouble();
    
    //capture numbers of years of investment from user
    System.out.print("Please enter years of investment (years): ");
    numberOfYears = keyboard.nextInt();
    
    //Calculate total money earned with formula p(1 + r/100)^n
    totalMoneyEarnedDouble = initialInvestmentMoney*(Math.pow(1 + interestRate/100, numberOfYears));
    
    //convert total money earned from double to integer
    int totalMoneyEarned = (int)totalMoneyEarnedDouble;
    
    //print total money earned
    System.out.println("The total money earned (dollars): " + totalMoneyEarned);
    }
}

20120128

Java Project #4: Travel Cost

/**
 * TravelCost computes the cost of travel using a vehicle.
 * 
 * @author (Andrew Samuels) 
 * @version (02-04-2010)
 */

//importing Scanner from library
import java.util.Scanner;

public class TravelCost
{
    public static void main(String[] args)
    {
    //create variables
    int travelDistance; //travel distance in miles
    int gasolineUsage; //gasoline usage in miles per gallon
    double gasolinePrice; //gasoline price in dollars per gallon
    double totalTravelCost; //The total cost of traveling
    
    //Create Scanner
    Scanner keyboard = new Scanner(System.in);
        
    //capture travel distance from user
    System.out.print("Please enter travel distance (miles): ");
    travelDistance = keyboard.nextInt();
    
    //capture gas usage from user
    System.out.print("Please enter gas usage of vehicle (miles per gallon): ");
    gasolineUsage = keyboard.nextInt();
    
    //capture gas price from user
    System.out.print("Please enter gas price (dollars per gallon): ");
    gasolinePrice = keyboard.nextDouble();
    
    //Calculate
    totalTravelCost = (travelDistance/gasolineUsage)*gasolinePrice;
    
    //print total travel cost
    System.out.println("The total travel cost (dollars): " + totalTravelCost);
    }
}

Java Project #3 : Student Grade

/**
 * StudentGrade determines the final semester score of students in a course.
 * 
 * @author (Andrew Samuels) 
 * @version (02-01-2010)
 */
//import scanner class
import java.util.Scanner;

public class StudentGrade
{

    public static void main(String[] args)
    {
        //initialize variables
        int programs;
        int homeworks;
        int examinations;
        double finalSemesterScore;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //input total program score from the user
        System.out.print("Please enter total program score (0-200): ");
        programs = keyboard.nextInt();
        
        //input total homework score from the user
        System.out.print("Please enter total homework score (0-500): ");
        homeworks = keyboard.nextInt();
        
        //input examination score from the user
        System.out.print("Please enter examination score (0-100): ");
        examinations = keyboard.nextInt();
        
        //calculate final semester score
        finalSemesterScore = 50*(programs*.005) + 30*(homeworks*.002) + 20*(examinations*.01);
        
        //print final semester score
        System.out.println("Final semester score is: " + finalSemesterScore);
        
        
    }
}

Java Project #2 : Coinjar

Got spare change in the piggie jar? Coinjar is a Java script that determines the value of coins in a jar. Tell it how many quarters, dimes, nickels, and pennies you have and it will spit out the dollar value.

/**
 * CoinJar determines the value of coins in a jar.
 * 
 * @author (Andrew Samuels) 
 * @version (02-01-2010)
 */
//import scanner class
import java.util.Scanner;

public class CoinJar
{

    public static void main(String[] args)
    {
        //create currency variables in pennies
        int quarters = 25;
        int dimes = 10 ;
        int nickels = 5;
        int pennies = 1;
        
        //create variables for user number of change
        int quartersNumber;
        int dimesNumber;
        int nickelsNumber;
        int penniesNumber;
        
        //create variables for value of change
        int quartersTotal;
        int dimesTotal;
        int nickelsTotal;
        int penniesTotal;
        
        //variables for total value as double
        int totalValue;
        double dollarCents;
        
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //input quarters from the user
        System.out.print("Please enter number of quarters: ");
        quartersNumber = keyboard.nextInt();
        
        //input dimes from the user
        System.out.print("Please enter number of dimes: ");
        dimesNumber = keyboard.nextInt();
        
        //input nickels from the user
        System.out.print("Please enter number of nickels: ");
        nickelsNumber = keyboard.nextInt();
        
         //input pennies from the user
        System.out.print("Please enter number of pennies: ");
        penniesNumber = keyboard.nextInt();
        
        //calculate quarters value
        quartersTotal = quarters*quartersNumber;
    
        //calculate dimes value
        dimesTotal = dimes*dimesNumber;
        
        //calculate nickels value
        nickelsTotal = nickels*nickelsNumber;
        
        //calculate pennies value
        penniesTotal = pennies*penniesNumber;
        
        //calculate total value in pennies
        totalValue = quartersTotal + dimesTotal + nickelsTotal + penniesTotal;
        
        //calculate total value as double in dollarCents ex. 2.19
        dollarCents = totalValue*(.01);
        
        //convert dollarCents to String for later use.
        String dollarCentsStr;
        dollarCentsStr = "" + dollarCents;
        
        //convert to dollarCents into integer
        int dollarCentsInt = (int)dollarCents;
        
        //separate cents using substring
        String number = dollarCentsStr;
        String result;
        result = number.substring(2);
       
        //print total dollars and cents
        System.out.println("Jar has " + dollarCentsInt + " dollars and " + result + " cents");
        
        
    }
}

Java Project #1 : The Sphere Problem

This is a straightforward problem. This little ditty computes the volume and surface area of a sphere. Java is fun, no?

/**
 * SphereProblem reads the radius of a sphere from the user and computes its volume and surface area, then prints them out..
 * 
 * @author (Andrew Samuels) 
 * @version (02-01-2010)
 */
//import scanner class
import java.util.Scanner;

public class SphereProblem
{

    public static void main(String[] args)
    {
        //initialize variables
        double radius; //radius of a sphere
        double volume; //volume of a sphere
        double area; //area of a sphere
        double PI = 3.1415;
        
        //create scanner
        Scanner keyboard = new Scanner(System.in);
        
        //input radius from the user
        System.out.print("Please enter radius of a sphere: ");
        radius = keyboard.nextDouble();
        
        //calculate volume
        volume = (4/3)*PI*radius*radius*radius;
    
        //calculate area
        area = 4*PI*radius*radius;
        
        //print volume
        System.out.println("Volume of sphere is: " + volume);
        
        //print area
        System.out.println("Area of sphere is: " + area);
        
    }
}