devotedlyannoyingcheesecake
devotedlyannoyingcheesecake
Nicole's Code Library
4 posts
Just some code that I wrote
Don't wanna be here? Send us removal request.
Text
To conserve space, I wrote all the programming challenges in a single file. The purpose of writing was not focused on error handling.
import java.util.Scanner; public class Chapter3 {    private static Scanner keyboard = new Scanner(System.in);    private static void challenge1() {        System.out.println("Enter a whole number within the range 1 - 10, inclusive.");        int number = keyboard.nextInt();        switch(number) {            case 1:                System.out.println("I");                break;            case 2:                System.out.println("II");                break;            case 3:                System.out.println("III");                break;            case 4:                System.out.println("IV");                break;            case 5:                System.out.println("V");                break;            case 6:                System.out.println("VI");                break;            case 7:                System.out.println("VII");                break;            case 8:                System.out.println("VIII");                break;            case 9:                System.out.println("IX");                break;            case 10:                System.out.println("X");                break;            default:                System.out.println("You didn't enter a whole number silly goose!");        }    }    private static void challenge2() {        System.out.println("Enter a date like so 6/10/60 so that the month times the day is equal to the year.");        String[] date = keyboard.nextLine().split("/");        if (Integer.parseInt(date[0]) * Integer.parseInt(date[1]) == Integer.parseInt(date[2])) {            System.out.println("MAGIC!");        }        else {            System.out.println("Not magic.");        }    }    private static void challenge3() {        System.out.println("Enter your weight in lbs and height in inches.");        String[] weightHeight = keyboard.nextLine().split("\\s");        double bmi = Integer.parseInt(weightHeight[0]) * 703/ Math.pow(Double.parseDouble(weightHeight[1]), 2);        if (bmi < 18.5) {            System.out.println("You're underweight.");        }        else if (bmi >= 18.5 && bmi <= 25) {            System.out.println("Your weight is optimal for your height and lifestyle.");        }        else if (bmi > 25){            System.out.println("You're overweight.");        }        else {            System.out.println("Unable to determine your BMI.");        }    }    private static void challenge4() {        System.out.println("Enter 3 test score grades.");        String[] grades = keyboard.nextLine().split("\\s");        double grade1 = Double.parseDouble(grades[0]);        double grade2 = Double.parseDouble(grades[1]);        double grade3 = Double.parseDouble(grades[2]);        double average = (grade1 + grade2 + grade3)/grades.length;        if (average >= 90 && average <= 100) {            System.out.println("average letter grade is A.");        }        else if (average >= 80 && average <= 89) {            System.out.println("average letter grade is B.");        }        else if (average >= 70 && average <= 79) {            System.out.println("average letter grade is C.");        }        else if (average >= 60 && average <= 69) {            System.out.println("average letter grade is D.");        }        else if (average <59){            System.out.println("average letter grade is F.");        }        else {            System.out.println("unable to determine average letter grade.");        }    }    private static void challenge5() {        System.out.println("Enter an objects mass in kilograms.");        double mass = Double.parseDouble(keyboard.nextLine());        double weight = mass * 9.8; // N        if (weight < 10) {            System.out.println("item is too light.");        }        else if (weight > 1000) {            System.out.println("item is too heavy.");        }        else {            System.out.println(weight);        }    }    private static void challenge6() {        System.out.println("Enter a number of seconds.");        double numberOfSeconds = Double.parseDouble(keyboard.nextLine());        if (numberOfSeconds >= 60 && numberOfSeconds < 3600) {            System.out.println(numberOfSeconds/60 + " minutes.");        }        else if (numberOfSeconds >= 3600 && numberOfSeconds < 86400) {            System.out.println(numberOfSeconds/3600 + " hours.");        }        else {            System.out.println(numberOfSeconds/86400 + " days.");        }    }    private static void challenge7() {        System.out.println("Enter three names.");        String[] names = keyboard.nextLine().split("\\s");        String[] ascendingNames = new String[names.length];        for (int i = 0; i < names.length; i++) {            for (int j = 0; j < names.length; j++) {                if (names[i].charAt(0) < names[j].charAt(0)) {                    ascendingNames[i] = names[i];                }            }        }        for (String name : ascendingNames) {            System.out.print(name + " ");        }    }    private static void challenge8() {        double packagePrice = 99;        System.out.println("Enter the amount of software packages you wish to purchase.");        int numberOfPackages = keyboard.nextInt();        double quantityDiscountPercentage = 0;        double discount = 0;        double total = 0;        if (numberOfPackages >= 10 && numberOfPackages <= 19) {            quantityDiscountPercentage = 0.2;            discount = packagePrice * quantityDiscountPercentage;            total = packagePrice - discount;            System.out.println(total);        }        else if (numberOfPackages >= 20 && numberOfPackages <= 49) {            quantityDiscountPercentage = 0.3;            discount = packagePrice * quantityDiscountPercentage;            total = packagePrice - discount;            System.out.println(total);        }        else if (numberOfPackages >= 50 && numberOfPackages <=99) {            quantityDiscountPercentage = 0.4;            discount = packagePrice * quantityDiscountPercentage;            total = packagePrice - discount;            System.out.println(total);        }        else if (numberOfPackages >= 100) {            quantityDiscountPercentage = 0.5;            discount = packagePrice * quantityDiscountPercentage;            total = packagePrice - discount;            System.out.println(total);        }        else {            System.out.println("Unable to determine the quantity discount.");        }    }    private static void challenge9() {        System.out.println("Enter the weight of your package.");        double weight = keyboard.nextDouble();        if (weight <= 2) {            System.out.println("shipping charge: $1.10");        }        else if (weight > 2 && weight < 6) {            System.out.println("shipping charge: $2.20");        }        else if (weight > 6 && weight < 10) {            System.out.println("shipping charge: $3.70");        }        else if (weight > 10) {            System.out.println("shipping charge: $3.80");        }        else {            System.out.println("unable to determine shipping charge.");        }    }    private static void challenge10() {        System.out.println("Enter the number of calories and fat grams in a food item.");        String[] caloriesAndFat = keyboard.nextLine().split("\\s");        double caloriesFromFat = Double.parseDouble(caloriesAndFat[1]) * 9;        double fatPercentage = caloriesFromFat * Double.parseDouble(caloriesAndFat[0]);        boolean lowFat = fatPercentage < 0.3;        System.out.println(fatPercentage * 100 + "% fat.");        if (lowFat) {            System.out.println("LOW FAT");        }    }    private static void challenge11() {        System.out.println("Enter 3 runner names and their time in minutes it took each of them to finish the race.");        String[] namesAndTimes = keyboard.nextLine().split("\\s");        String[] names = {namesAndTimes[0], namesAndTimes[2], namesAndTimes[4]};        double[] times = {Double.parseDouble(namesAndTimes[1]), Double.parseDouble(namesAndTimes[3]), Double.parseDouble(namesAndTimes[5])};        for (int i = 0; i < times.length; i++) {            for (int j = 0; j < times.length; j++) {                String tempName = "";                double tempTime = 0;                if (times[i] < times[j]) {                    tempName = names[i];                    tempTime = times[i];                    names[i] = names[j];                    times[i] = times[j];                    names[j] = tempName;                    times[j] = tempTime;                }            }        }        int timesIndex = 0;        for(String name : names) {            System.out.println(name + " " + times[timesIndex]);            timesIndex++;        }    }    private void challenge12() {        int airSpeedPerSecond = 1100; // feet per second        int waterSpeedPerSecond = 4900;        int steelSpeedPerSecond = 16400;        System.out.println("Enter the medium (air,water,steel) and the distance the sound wave will travel.");        String[] mediumAndDistance = keyboard.nextLine().split("\\s");        String medium = mediumAndDistance[0];        double distance = Double.parseDouble(mediumAndDistance[1]);        double time = 0;        switch(medium) {            case "air":                time = distance / airSpeedPerSecond;                System.out.println(time);                break;            case "water":                time = distance / waterSpeedPerSecond;                System.out.println(time);                break;            case "steel":                time = distance / steelSpeedPerSecond;                System.out.println(time);                break;            default:                System.out.println("Unable to determine the time.");                break;        }    }    private static void challenge13() {        System.out.println("Enter the letter of your package and the number of hours used.");        String[] letterAndHours = keyboard.nextLine().split("\\s");        String letter = letterAndHours[0];        double hours = Double.parseDouble(letterAndHours[1]);        double pricePerMonth;        double pricePerHour;        double total;        switch(letter) {            case "A":                pricePerMonth = 9.95; // for 10 hours                pricePerHour = 2; // after 10 hours                if (hours > 10) {                    double extraHours = hours - 10;                    total = pricePerMonth + extraHours * pricePerHour;                } else {                    total = pricePerMonth;                }                System.out.println(total);                challenge14("A", total, hours);                break;            case "B":                pricePerMonth = 13.95; // for 20 hours                pricePerHour = 1; // after 20 hours                if (hours > 20) {                    double extraHours = hours - 10;                    total = pricePerMonth + extraHours * pricePerHour;                } else {                    total = pricePerMonth;                }                System.out.println(total);                challenge14("B", total, hours);                break;            case "C":                pricePerMonth = 19.95; // unlimited access                total = pricePerMonth;                System.out.println(total);                break;            default:                System.out.println("Unable to determine total.");                break;        }    }    private static void challenge14(String packageLetter, double total, double hours) {        double couldHaveSavedWithB;        double bPackagePrice;        double couldHaveSavedWithC;        double bPackagePricePerMonth = 13.95; // per month for 20 hours        double costPerExtraHour = 1; // per hour        double cPackagePrice = 19.95; // per month        switch(packageLetter) {            case "A":                // B package                if (hours > 20) {                    double extraHours = hours - 20;                    bPackagePrice = costPerExtraHour * extraHours + bPackagePricePerMonth;                }                else {                    bPackagePrice = bPackagePricePerMonth;                }                if (total > bPackagePrice) {                    couldHaveSavedWithB = total - bPackagePrice;                    System.out.println("Could have saved " + couldHaveSavedWithB);                }                else {                    System.out.println("This package seems to be a good fit.");                }                // C package                if (total > cPackagePrice) {                    couldHaveSavedWithC = total - cPackagePrice;                    System.out.println("Could have saved " + couldHaveSavedWithC);                }                break;            case "B":                // C package                if (total > cPackagePrice) {                    couldHaveSavedWithC = total - cPackagePrice;                    System.out.println("Could have saved " + couldHaveSavedWithC);                }                else {                    System.out.println("This package seems to be a good fit.");                }                break;            default:                System.out.println("Huh?");                break;        }    }    private static void challenge15() {        double monthlyFee = 10;        double checkFee;        double total;        System.out.println("Enter the number of checks written for the month.");        int numberOfChecks = keyboard.nextInt();        if (numberOfChecks > 0 && numberOfChecks < 20) {            checkFee = 0.1;            total = monthlyFee + numberOfChecks * checkFee;        }        else if (numberOfChecks >= 20 && numberOfChecks <= 39) {            checkFee = 0.08;            total = monthlyFee + numberOfChecks * checkFee;        }        else if (numberOfChecks >= 40 && numberOfChecks <= 59) {            checkFee = 0.06;            total = monthlyFee + numberOfChecks * checkFee;        }        else if (numberOfChecks >= 60) {            checkFee = 0.04;            total = monthlyFee + numberOfChecks * checkFee;        }        else {            total = monthlyFee;        }        System.out.println(total);    }    private static void challenge16() {        System.out.println("Enter the number of books you purchased this month.");        int numberOfBooks = keyboard.nextInt();        int numberOfPoints = 0; // default if purchased books is 0        if (numberOfBooks == 1) {            numberOfPoints = 5;        }        else if (numberOfBooks == 2) {            numberOfPoints = 15;        }        else if (numberOfBooks == 3) {            numberOfPoints = 30;        }        else if (numberOfBooks >= 4) {            numberOfPoints = 60;        }        System.out.println(numberOfPoints);    }    public static void main(String[] args) {        // insert desired method calls    } }
1 note · View note
Text
For less space consumption, I threw all the challenges into a single file. I kept it simple because the purpose of writing this was not to work on error handling.
import java.util.*; public class Chapter2 {    private static Scanner keyboard = new Scanner(System.in);    private static void challenge1(){        String name = "Nicole Binette";        int age = 21;        double annualPay = 100000;        System.out.println("My name is " + name + ", my age is " + age + " and I hope to earn $" +                annualPay + " per year.");    }    private static void challenge2() {        String firstName = "Nicole", middleName = "Amporn", lastName = "Binette";        char firstInitial = 'N', middleInitial = 'A', lastInitial = 'B';        System.out.println(firstInitial + " " + middleInitial + " " + lastInitial);        System.out.println(firstName + " " + middleName + " " + lastName);    }    private static void challenge3() {        System.out.println("Nicole" + "\n16410 NE 99th ST Redmond, WA 98052" + "\n425-951-9256" +                "\nComputing and Software Development");    }    private static void challenge4() {        System.out.println("\t\t\t*");        System.out.println("\t\t   ***");        System.out.println("\t\t  *****");        System.out.println("\t\t *******");        System.out.println("\t\t  *****");        System.out.println("\t\t   ***");        System.out.println("\t\t\t*");    }    private static void challenge5() {        double salesDivisionsPercentage = 0.62;        double salesEstimatedGeneration = 4.6 * 0.62;    }    private static void challenge6() {        double acreInSquareFeet = 43560;        double numAcres = 389767 / acreInSquareFeet;    }    private static void challenge7() {        System.out.println("Enter purchase amount.");        double amount = keyboard.nextDouble();        double stateSalesTax = 0.04;        double countySalesTax = 0.02;        double totalSalesTax = stateSalesTax + countySalesTax;        double total = amount * totalSalesTax + amount;        System.out.println("amount: " + amount);        System.out.println("state tax: " + stateSalesTax);        System.out.println("county tax: " + countySalesTax);        System.out.println("total tax: " + totalSalesTax);        System.out.println("total: " + total);    }    private static void challenge8() {        double cookiesPerBag = 40;        double servingsPerBag = 10;        int calPerServing = 300;        System.out.println("Enter number of cookies eaten.");        double numberOfCookiesEaten = keyboard.nextDouble();        double totalCalories = numberOfCookiesEaten * (servingsPerBag / cookiesPerBag) * calPerServing;        System.out.println("Calories consumed: " + totalCalories);    }    private static void challenge9() {        System.out.println("Enter number of miles driven.");        double milesDriven = keyboard.nextDouble();        System.out.println("Enter gallons of gas used.");        double gallonsOfGasUsed = keyboard.nextDouble();        double mpg = milesDriven / gallonsOfGasUsed;        System.out.println(mpg);    }    private static void challenge10() {        System.out.println("Enter test scores separated by a space."); //Indicate the number of tests with that score with x. For example, 3 tests with a perfect score would be 100x3");        String[] tests = keyboard.nextLine().split("\\s");        Map<Double, Integer> testScores = new HashMap<>();        for (String string : tests) {            String[] test = string.split("x");            testScores.put(Double.parseDouble(test[0]), Integer.parseInt(test[1]));        }        Object[] scores = testScores.keySet().toArray();        Object[] numberOfTestsPerScore = testScores.values().toArray();        double total = 0;        int numberOfTests = 0;        for (int i = 0; i < scores.length; i++) {            total += (Double) scores[i] * (Integer) numberOfTestsPerScore[i];            numberOfTests += (Integer) numberOfTestsPerScore[i];        }        double average = total / numberOfTests;        System.out.println("average test score " + average);    }    private static void challenge11() {        System.out.println("Enter the retail price for a circuit board.");        double retailPrice = keyboard.nextDouble();        double percentProfit = 0.4;        double profit = retailPrice / percentProfit;        System.out.println("profit " + profit);    }    private static void challenge12() {        System.out.println("Enter the name of your favorite city.");        String favCity = keyboard.nextLine();        System.out.println(favCity.length() + " " + favCity.toUpperCase() + " " + favCity.toLowerCase() + " " + favCity.charAt(0));    }    private static void challenge13() {        double taxPercent = 0.0675;        System.out.println("Enter the price for the meal.");        double mealCharge = keyboard.nextDouble();        double mealChargeWithTax = mealCharge + taxPercent * mealCharge;        double tipPercent = 0.2;        double totalMealCharge = mealChargeWithTax + tipPercent * mealChargeWithTax;        System.out.println("total meal charge " + totalMealCharge);    }    private static void challenge14() {        System.out.println("Enter the number of males and females in registered for a class. Like so M F");        String[] maleAndFemale = keyboard.nextLine().split("\\s");        int male =  Integer.parseInt(maleAndFemale[0]);        int female = Integer.parseInt(maleAndFemale[1]);        int totalRegistered = male + female;        double percentMale = ((double) male / totalRegistered) * 100;        double percentFemale = ((double) female / totalRegistered) * 100;        System.out.println("percent male " + percentMale + "%" + "\npercent female " + percentFemale + "%");    }    private static void challenge15() {        int numOfStockShares = 600;        double pricePerShare = 21.77;        double percentCommission = 0.02;        double priceForStock = numOfStockShares * pricePerShare;        double commission = priceForStock * percentCommission;        double totalPrice = priceForStock + commission;        System.out.println("total price for stocks including commission " + totalPrice);    }    private static void challenge16() {        int customersSurveyed = 12467;        double percentConsuming1plus = 0.14;        double percentPartialToCitrus = 0.64;        double numPurchasing1plus = customersSurveyed * percentConsuming1plus;        double numPartialToCitrus = customersSurveyed * percentPartialToCitrus;        System.out.println("number of customers who purchase 1+ energy drinks per week " + numPurchasing1plus);        System.out.println("number of customers who are partial to citrus flavored energy drinks " + numPartialToCitrus);    }    private static void challenge17() {        // cookie recipe        double cupsSugar = 1.5;        double cupsButter = 1;        double cupsFlour = 2.75;        int makesCookies = 48;        System.out.println("Enter number of cookies you wish to make.");        int toMakeCookies = keyboard.nextInt();        double quotient = (double) toMakeCookies / makesCookies;        // adjust ingredients        System.out.println("cups of sugar " + cupsSugar * quotient + "\ncups of butter " + cupsButter * quotient +                "\ncups of flour " + cupsFlour * quotient);    }    private static void challenge18() {        System.out.println("Enter your name, your age, the name of a city, the name of a college, a profession," +                " a type of animal," +                "and a pet's name.");        String[] words = keyboard.nextLine().split(",\\s");        System.out.println("There once was a person named " + words[0] + " who lived in " + words[1] +                ". At the age of " + words[2] + ", " + words[0] + " went to college at " + words[3] +                ". " + words[0] + " graduated and went to work as a " + words[4] + ". Then, " + words[0] +                "adopted a(n)" + words[5] + " named " + words[6] + ". They both lived happily ever after!");    }    private static void challenge19() {        int numberOfShares = 1000;        double pricePerShare = 32.87;        double percentCommission = 0.02;        double commission = (numberOfShares * pricePerShare) * percentCommission;        double total = numberOfShares * pricePerShare + commission;        double salePricePerShare = 33.92;        double saleCommission = (numberOfShares * salePricePerShare) * percentCommission;        double totalSale = numberOfShares * salePricePerShare + saleCommission;        double profit = totalSale - total;        System.out.println("profit " + profit);    }    public static void main(String args[]) {        challenge1();        challenge2();        challenge3();        challenge4();        challenge7();        challenge8();        challenge9();        challenge10();        challenge11();        challenge12();        challenge13();        challenge14();        challenge15();        challenge16();        challenge17();        challenge18();        challenge19();    } }
0 notes
Text
//My first Java Program
public class MyFirstJavaProgram {
     public static void main(String[] args) {
         System.out.println(”Hello World!”); 
    }
}
0 notes
Text
Where do I get my ideas and problems?
My school suggested the book, “Starting out with Java from Control Structures through Data Structures” by Tony Gaddis and Godfrey Muganda, to learn about the programming language Java so that is where I get my ideas and problems. My professor’s are also computer gods. If you’re interested in learning Java I highly recommend this book because it covers some basics and gets into more advanced stuff too.
I could just save these as files on my laptop, but I figure I might need room on my laptop to store other things. Plus, I might help inspire someone. Maybe someone looking for a software developer will like my style and hire me.
My school also suggested the book, “C# 6.0 and the .NET 4.6 Framework” by Andrew Troelsen and Philip Japikse. This book doesn’t have practice problems, but it does cover the basics and advanced stuff. I think a good follow up after reading that book would be, “Design Patterns in C#” by Steven John Metsker. I haven’t started the book, but design patterns are a good concept to know about when starting to write your own software.
Once I finish these books I plan to learn about some testing through the book suggested by my father, “Testing Computer Software” by Cem Kaner, Jack Falk, and Hung Quoc Nguyen. I haven’t done a lot with testing, but I do believe that it’s important to know about it.
Last but not least, “Agile Software Development: Principles, Patterns, and Practices” by Robert C. Martin is also on my list since Agile is a common development strategy in the industry. It also contains some C++ (which I still need to learn) and Java code. 
Some other books I would like to review that were suggested by my school are, “MySQL” and “PHP/MySQL” by Murach. 
1 note · View note