Thursday, October 15, 2015

Java Fundamentals Homework - Sum the Numbers from 1 to N


Hello,

This is my first ever blog, so I decided what better to post, than my homework from Java Fundamentals which is due on 20.10.15. I hope my solutions can be of some assistance to
whoever is reading. Thank you for your time. Lets begin with the first Problem from the homework.


Problem: Sum the Numbers from 1 to N


Create a Java program that reads a number N from the console and calculates the sum of all numbers from 1 to N (inclusive).




import java.util.Scanner;

public class Sum1toN {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        int number = Integer.parseInt(console.nextLine());
        int increase = 0;

        for (int i = 1; i <=number; i++){
            increase += i;
            }
        System.out.print(increase);
        }
    }

Java Fundamentals Homework - Get Average


Problem: Get Average

Create a method that finds the average of three numbers. Read in internet about java methods. Check the naming conventions. Invoke your method and test it. Format the output to two digits after the decimal separator. The placeholder is %.2f





import java.util.Scanner;

public class GetAverage {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        double a = Double.parseDouble(console.nextLine());
        double b = Double.parseDouble(console.nextLine());
        double c = Double.parseDouble(console.nextLine());

        double averageOfNums = GetAverage(a, b, c);
        System.out.println(String.format("%.2f", averageOfNums));
    }

    private static double GetAverage(double a, double b, double c) {

        double averageOfNums = (a + b + c)/3.0;
        return averageOfNums;
    }
}

Java Fundamentals Homework - Ghetto Numeral System


Problem: Ghetto Numeral System

Write a program that converts the decimal number system to the ghetto numeral system. In the ghetto, numbers are represented as following:


import java.util.Scanner;

public class GhettoNumeralSystem {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        String input = console.nextLine();
        String ghetto = "";
        GhettoNames(input, ghetto);
        }

    private static void GhettoNames(String input, String ghetto) {
        for (int i = 0; i <input.length() ; i++) {
            switch (input.charAt(i)) {
                case '0': ghetto += "Gee";
                    break;
                case '1': ghetto += "Bro";
                    break;
                case '2': ghetto += "Zuz";
                    break;
                case '3': ghetto += "Ma";
                    break;
                case '4': ghetto += "Duh";
                    break;
                case '5': ghetto += "Yo";
                    break;
                case '6': ghetto += "Dis";
                    break;
                case '7': ghetto += "Hood";
                    break;
                case '8': ghetto += "Jam";
                    break;
                case '9': ghetto += "Mack";
                    break;
            }
        }
        System.out.println(ghetto);
        System.out.println("it's me, Wombat!");
    }
}

Java Fundamentals Homework - Print a Character Triangle


Problem: Print a Character Triangle


Print the characters from ‘a’ to ‘z’ on the console on a single line, separated by a space. Use a for-loop. Note: you can directly declare and increment char in the for-loop. for (char c = ‘a’; …)


import java.util.Scanner;

public class PrintACharachterTriangle {
    public static void main(String[] args) {

        Scanner console = new Scanner(System.in);

        int number = Integer.parseInt(console.nextLine());
        for (int i = 1; i <= number ; i++) {
            for (int j = 0; j<i; j++){
                System.out.print((char) (j + 97) + " ");
            }
            System.out.println();
        }
        for (int i = number - 1; i > 0 ; i--) {
            for (int j = 0; j <i ; j++) {
                System.out.print((char) (j + 97) + " ");
            }
            System.out.println();
        }
    }
}

Java Fundamentals Homework - Assign Variables


Hello,

In this post I'm going to submit my Java Fundamentals Homework which is due on 20.10.2015.

I hope, I can help someone with my solutions.

Problem: Assign Variables

Find suitable types for variables. You are given the following types: byte, short, int, long, char, boolean, float, double, and String. Assign the following values to them false, “Palo Alto, CA”, 32767, 2000000000, 0.1234567891011, 0.5f, 919827112351L, 127, ‘c’. Try to assign 32768 to short and see what happens.






import java.util.Scanner;

public class AssignVariables {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        int integerValue = 2000000000;
        char characterValue = 'c';
        boolean boolValue = false;
        double doubleValue = 0.1234567891011;
        String stringVAlue = "Palo Alto, CA";
        short shortVAlue = 32767;
        long longVslue = 919827112351L;
        float floatValue = 0.5f;
        byte byteValue = 127;

        System.out.println(integerValue + ", " + doubleValue);
        System.out.println(characterValue + ", " + boolValue + ", " + stringVAlue);
        System.out.println(shortVAlue + ", " + longVslue);
        System.out.println(byteValue + ", " + floatValue);


    }
}