Skip to content
Snippets Groups Projects
Main.java 4.2 KiB
Newer Older
Jeremias Shadbolt's avatar
Jeremias Shadbolt committed
package fi.utu.tech;

import java.util.List;
import java.util.Scanner;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

interface neliöi {
    int neliö(int x);
}
public class Main {

    /**
     * Main class.
     * @param args Command line arguments
     */
    public static void main(String[] args) throws Exception {

        tehtA();
        tehtB();
        tehtC();
        tehtD();

        System.out.print("Neliöitävä luku: ");
        var luku = new Scanner(System.in).nextInt();
        tehtE(luku);
    }


    /**
     *
     * Teht a)
     *
     * @.pre true
     * @.post list(FOREACH(System.out.print())
     */
    public static void tehtA() {
        var lista1 = List.of(1, 2, 3);
        var lista2 = List.of(1, 2, 3);
        var virta1 = lista1.stream();
        var virta2 = lista2.stream();

        var temp = Stream.concat(virta1, virta2);
        var ans = temp.collect(Collectors.toList());
        for (int i : ans) System.out.print(i);
        System.out.print("\n" + ans);
    }

    /**
     * Teht b)
     *
     * @.pre true
     * @.post print(list(FOREACH:index * 2 && index != 4))
     */
    public static void tehtB() {

        var lista1 = List.of(1, 2, 3);
        var lista2 = List.of(1, 2, 3);
        var virta1 = lista1.stream();
        var virta2 = lista2.stream();

        var temp = Stream.concat(virta1, virta2);
        var temp1 = temp.collect(Collectors.toList());

        var ans = temp1.stream()
                            .map(i -> i*2)
                            .filter(i -> i!=4);

        var lista = ans.collect(Collectors.toList());
        System.out.println(lista);

    }

    /**
     * Teht c)
     *
     * @.pre true
     * @.post print(list: maxValue)
     */
    public static void tehtC(){

        var lista1 = List.of(1, 2, 3);
        var lista2 = List.of(1, 2, 3);
        var virta1 = lista1.stream();
        var virta2 = lista2.stream();

        var temp = Stream.concat(virta1, virta2);
        var temp1 = temp.collect(Collectors.toList());

        int max = temp1.stream()
                    .max(Integer::compareTo).get();

        System.out.println(max);
    }

    /**
     * Teht d)
     *
     * @.pre
     * @.post print(list(index) + "\n"))
     */
    public static void tehtD(){
        var lista1 = List.of(1, 2, 3);
        var lista2 = List.of(1, 2, 3);
        var virta1 = lista1.stream();
        var virta2 = lista2.stream();

        var temp = Stream.concat(virta1, virta2);
        var temp1 = temp.collect(Collectors.toList());

        temp1.forEach(System.out::println);

    }

    /**
     *
     * @param luku
     * @.pre luku!=null
     * @.post true
     *
     * @.pre param > 0
     * @.post print(param*param) (squared)
     *
     *
     * @throws Exception
     */
    public static void tehtE(int luku)throws Exception{
        tehtE e = new tehtE();
        neliöi n = (int x) -> x*x;
        anon a = new anon();
        Function<Integer, Integer> neliöijä = x -> x*x;
        int ans = n.neliö(luku);

    //ilmeisesti teht f??
        try {
            System.out.println(e.neliöi(luku));
        } catch (Exception exception){
            throw new Exception("ei skulaa nyt koska " + exception.getMessage());
        }
        try{
            System.out.println(luku + " * " + luku + " = " + neliöijä.apply(luku));
        } catch (Exception exception){
            throw  new Exception("Homma ei rokkaa koska " + exception.getMessage());
        }

        try{
            System.out.println(ans);
        }catch (Exception exception){
            throw new Exception("Ei ihan menny putkeen koska " + exception.getMessage());
        }

        try{
            System.out.println(a.neliöi(luku));
        }catch (Exception exception){
            throw new Exception("Ei toimi koska " + exception.getMessage());
        }
    }
    }

/**
 * Anonymous class in main-class to return square of user input value
 *
 * @.pre luku != null
 * @.post return luku * luku
 *
 */
class anon implements neliöijä{

        public int neliöi(int luku){
            Function<Integer, Integer> neliöi = x -> luku*luku;
            int ans = neliöi.apply(luku);
            return ans;
        }
    }