Java Homework part 1

Java Homework part 1

Java

 

These posts will be a little simpler I'll just post short descriptions on what what was supposed to be done and the source code for my solutions below. Certain things in my solutions will be in Finnish due to course requirements.

Our first homework assignment was 10 little tasks to complete with Java. I used the required Eclipse Oxygen editor with Java Development kit version 1.8 to write these.

Basic Stuff

1/10

Create a calculator that calculates the amount of "daily fine" based on user income

 

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class PaivaSakko {

public static void main(String[] args) {

int tulot = 0;

int sakko = 0

Scanner input = new Scanner(System.in);

System.out.print("Anna nettotulot: ");

tulot = input.nextInt();

sakko = (tulot-255)/60;

System.out.print("Nettotuloilla " + tulot + " päiväsakko on " + sakko + "euroa");

DecimalFormat desimaalit = new DecimalFormat("0.00");

System.out.println("Nettotuloilla " + tulot + " päiväsakko on " + desimaalit.format(sakko) + "euroa");

}

}

2/10

Create a program that calculates how much a driven kilometer costs per refuel, driven kilometers and gas price.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Bensa {

public static void main(String[] args) {

DecimalFormat desimaalit = new DecimalFormat("0.00");

double GasInput = 0; // Tankattu bensa

int AmountDriven = 0; //ajettu määrä

double PerLitre = 0; // syöttetty litrahinta

double TankkausHinta = 0;

// create a scanner type olio, that can read

// user input

Scanner input = new Scanner(System.in);

System.out.print("Anna tankattu määrä: ");

GasInput = input.nextDouble(); //Lukee seuraavan kokonaisluvun

System.out.print("Anna ajetut kilometrit: ");

AmountDriven = input.nextInt(); //Lukee ajetun matkan

System.out.print("Anna litrahinta: ");

PerLitre = input.nextDouble();

TankkausHinta = (GasInput/AmountDriven)*PerLitre;

System.out.print("Ajo per kilometri maksaa " + desimaalit.format(TankkausHinta));

}

}

3/10

A program that calculates how long a trip takes.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Matka {

public static void main(String[] args) {

DecimalFormat desimaalit = new DecimalFormat("0.00");

int DriveAmount = 0;

int AvgSpeed = 0;

double DriveTime = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna matka: ");

DriveAmount = input.nextInt(); // reads Amount Driven

System.out.print("Anna nopeus: ");

AvgSpeed = input.nextInt();

DriveTime = (double)DriveAmount/AvgSpeed;

System.out.print("Aikaa menee: " + desimaalit.format(DriveTime) + " tuntia");

}

}

4/10

A program that calculates a discounted price based on user input of original price and discount percentage.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Alennus {

public static void main(String[] args) {

DecimalFormat desimaalit = new DecimalFormat("0.00");

double NoDiscount = 0;

int discount = 0;

double CountDiscount = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna alentamaton hinta: ");

NoDiscount = input.nextDouble(); //Lukee seuraavan kokonaisluvun

System.out.print("Anna alennusprosentti: ");

discount = input.nextInt();

CountDiscount = NoDiscount*(100-discount)/100;

System.out.print("Alennettu hinta on " + desimaalit.format(CountDiscount));

}

}

5/10

A program that calculates gas price per person in car.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Kimppakyyti {

public static void main(String[] args) {

DecimalFormat desimaalit = new DecimalFormat("0.00");

int KlmDriven = 0;

double LtrperKm = 0;

double GasPrice = 0;

int PersAmount = 0;

double CostperPerson = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna ajetut kilometrit: ");

KlmDriven = input.nextInt(); //Lukee seuraavan kokonaisluvun

System.out.print("Anna kulutus per 100 km: ");

LtrperKm = input.nextDouble();

System.out.print("Anna polttoaineen litrahinta: ");

GasPrice = input.nextDouble();

System.out.print("Anna kimppakyytiläisten lukumäärä: ");

PersAmount = input.nextInt();

CostperPerson = KlmDriven*LtrperKm/100*GasPrice/PersAmount;

System.out.print("Bensakustannus per henkilö on " + desimaalit.format(CostperPerson) + "euroa");

}

}

If statements in Java

6/10

A program that calculates salary that the user has left after taxes and some retirement and unemployment payments that get taken from your salary in Finland.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Palkka {

public static void main(String[] args) {

int BrutSalary = 0;

double TaxPerc = 0;

int UserAge = 0;

double TaxAmount = 0;

double UnEmploy = 0.0115;

double PayedEmploy = 0;

double RetiredFund = 0;

double SalaryLeft = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna palkka: ");

BrutSalary = input.nextInt();

System.out.print("Anna veroprosentti: ");

TaxPerc = input.nextDouble();

System.out.print("Anna ikä: ");

UserAge = input.nextInt();

TaxAmount = BrutSalary*TaxPerc/100;

PayedEmploy = BrutSalary*UnEmploy;

if (UserAge >= 53) {

RetiredFund = BrutSalary*0.0705;

}

else {

RetiredFund = BrutSalary*0.0555;

}

SalaryLeft = BrutSalary-TaxAmount-PayedEmploy-RetiredFund;

DecimalFormat desimaalit = new DecimalFormat("0.00");

System.out.println("Bruttopalkka " + BrutSalary);

System.out.println("Veron osuus " + desimaalit.format(TaxAmount));

System.out.println("Työeläkevakuutusmaksun osuus " + desimaalit.format(RetiredFund));

System.out.println("Työttömyysvakuutuksen osuus " + desimaalit.format(PayedEmploy));

System.out.print("Käteen jää " + desimaalit.format(SalaryLeft));

}

}

7/10

A program that calculates the users paid tax that's paid to fund our national TV-company "Yletax".

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Ylevero {

public static void main(String[] args) {

int UserAge = 0;

double UserIncome = 0;

double YleTax = 0.68;

double TaxAmount = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna vuositulosi ja ikäsi: ");

UserIncome = input.nextDouble();

UserAge = input.nextInt();

TaxAmount = UserIncome*YleTax/100;

if (UserAge < 18) {

TaxAmount = 0;

}

else if (TaxAmount > 140) {

TaxAmount = 140;

}

else if (TaxAmount < 70) {

TaxAmount = 0;

}

DecimalFormat desimaalit = new DecimalFormat("0.00");

System.out.print("Ylevero on " + desimaalit.format(TaxAmount));

}

}

8/10

A program that compares the price between a single ticket for multiple trips and a single payment ticket for a months trips.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Matkavertailu {

public static void main(String[] args) {

int TripAmount = 0;

double SingleTicket = 0;

double MonthTicket = 0;

double PriceSingle = 0;

Scanner input = new Scanner(System.in);

System.out.print("Montako matkaa teet kuukaudessa: ");

TripAmount = input.nextInt();

System.out.print("Anna yksittäisen lipun hinta: ");

SingleTicket = input.nextDouble();

System.out.print("Anna kuukausilipun hinta: ");

MonthTicket = input.nextDouble();

DecimalFormat desimaalit = new DecimalFormat("0.00");

PriceSingle = SingleTicket*TripAmount;

if (PriceSingle > MonthTicket) {

System.out.print("Kuukausilippu on " + desimaalit.format(PriceSingle-MonthTicket) + " euroa halvempi kuin yksittäinen");

}

else if (PriceSingle < MonthTicket) {

System.out.print("Yksittäinen on " + desimaalit.format(MonthTicket-PriceSingle) + " euroa halvempi kuin kuukausilippu");

}

}

}

9/10

A program that discounts the cheapest pizza off the total price and then calculates the cost per pizza.

import java.text.DecimalFormat;

import java.util.Scanner;

public class Pizza {

public static void main(String[] args) {

double PizzaPrice1 = 0;

double PizzaPrice2 = 0;

double PizzaPrice3 = 0;

double FinalPrice = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna pizzojen hinnat (3): ");

PizzaPrice1 = input.nextDouble();

PizzaPrice2 = input.nextDouble();

PizzaPrice3 = input.nextDouble();

FinalPrice = PizzaPrice1+PizzaPrice2+PizzaPrice3;

if (PizzaPrice1 < PizzaPrice2 && PizzaPrice1 < PizzaPrice3) {

FinalPrice = PizzaPrice1+PizzaPrice2+PizzaPrice3-PizzaPrice1;

}

else if (PizzaPrice2 < PizzaPrice1 && PizzaPrice2 < PizzaPrice3) {

FinalPrice = PizzaPrice1+PizzaPrice2+PizzaPrice3-PizzaPrice2;

}

else if (PizzaPrice3 < PizzaPrice1 && PizzaPrice3 < PizzaPrice2) {

FinalPrice = PizzaPrice1+PizzaPrice2+PizzaPrice3-PizzaPrice3;

}

DecimalFormat desimaalit = new DecimalFormat("0.00");

System.out.println("Maksettavaa: " + desimaalit.format(FinalPrice));

System.out.print("Yksittäisen hinta: " + desimaalit.format(FinalPrice/3));

}

}

10/10

A program that calculates the users fine based off the speed limit of the area and the amount the user was speeding in.

 

import java.text.DecimalFormat;

import java.util.Scanner;

public class Rikesakko {

public static void main(String[] args) {

int SpeedLimit = 0;

int UserSpeed = 0;

Scanner input = new Scanner(System.in);

System.out.print("Anna nopeusrajoitus: ");

SpeedLimit = input.nextInt();

System.out.print("Anna nopeutesi: ");

UserSpeed = input.nextInt();

if (SpeedLimit <= 60 && SpeedLimit >= 10 && UserSpeed-SpeedLimit <= 15) {

System.out.print("Rikesakko on 170" );

}

else if (SpeedLimit <= 60 && SpeedLimit >= 10 && UserSpeed-SpeedLimit > 15

&& UserSpeed-SpeedLimit <= 20) {

System.out.print("Rikesakko on 200" );

}

else if (SpeedLimit <=120 && SpeedLimit >= 70 && UserSpeed-SpeedLimit <= 15){

System.out.print("Rikesakko on 140" );

}

else if (SpeedLimit <= 120 && SpeedLimit >= 70 && UserSpeed-SpeedLimit > 15

&& UserSpeed-SpeedLimit <= 20) {

System.out.print("Rikesakko on 200" );

}

else if (UserSpeed-SpeedLimit > 20)

System.out.print("Menee päiväsakoille");

}

}

I found these exercises to be easy. But I guess they should be as the course just started.

Sources:

  • image from: https://svgsilh.com/image/311945.html