Friday, February 1, 2019

Java Templates!

Java I/O
import java.util.Scanner;

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

        String name;
        int age = 99;
        Scanner in = new Scanner(System.in);

        System.out.println ("Hello, Please enter your name and age.");
        name = in.nextLine();
        age = in.nextInt();
        System.out.println ("Hello, " + name + ", \n\tyou are " + age + " years old.");

    }
}
Java IO Files
import java.io.*;
import java.util.*;
public class outofplace {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader("data.in"));
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter("data.out")));

        int n = Integer.parseInt(br.readLine());
        int[] height = new int[n];
        int[] width = new int[n];

        System.out.println (n);

        StringTokenizer st;
        for (int i = 0; i < n; i++) {
            st = new StringTokenizer(br.readLine());
            height[i] = Integer.parseInt(st.nextToken());
            width[i] = Integer.parseInt(st.nextToken());
        }

        br.close();
        pw.close();
    }
}
Java Data Structures
import java.util.Scanner;
import java.util.List;
import java.util.*;
import java.awt.*;

public class ds {
    java.util.List<String> slist = new ArrayList();
    public static void main ( String[] args ) {
        Graph gr = new Graph();
        Vertex a = new Vertex( "a" );
        Vertex b = new Vertex( "b" );
        Vertex c = new Vertex( "c" );
        Vertex d = new Vertex( "d" );
        Vertex e = new Vertex( "e" );
        Vertex f = new Vertex( "f" );

        Set vertices = gr.getVertices();
        if (vertices == null) {
            vertices = new HashSet();
            vertices.add(a);
            vertices.add(b);
            vertices.add(c);
            vertices.add(d);
            vertices.add(e);
            vertices.add(f);
        }
        Edge2 ae = new Edge2 (a,e,3);
        Edge2 ac = new Edge2 (a,c,1);
        Edge2 cf = new Edge2 (c,f,9);
        // so on and so forth

        Set edges = gr.getEdges();
        if (edges == null) {
            edges = new HashSet();
            edges.add(ae);
            edges.add(ac);
            edges.add(cf);
        }
    }
}

class Node
{
    List<Node> edges;
    java.awt.List awtedges;
    int id;
}

class Edge
{
    Node endA;
    Node endB;
}

class Edge2 {
    private Vertex from;
    private Vertex to;
    private int weight;

    public Edge2 (Vertex from, Vertex to, int weight) {
        this.from = from;
        this.to = to;
        this.weight = weight;
    }
}

class Vertex
{
    private String name;
    public Vertex (String name) {
        this.name = name;
    }
}

class Graph
{
    private Set<Vertex> vertices;
    private Set<Edge> edges;
    private Map<Vertex, List<Edge>> adj;

    // put getter, setter

    public Graph () {
        System.out.println( "create a graph." );
    }

    public Graph ( Set<Vertex> vertices, Set<Edge> edges, Map<Vertex, List<Edge>> adj) {
        System.out.println( "create a graph with inputs." );
        // super();
        this.vertices = vertices;
        this.edges = edges;
        this.adj = adj;
    }
    public Set<Edge2> getEdges () {
        return this.edges;
    }

    public Set<Vertex> getVertices() {
        return this.vertices;
    }
}

No comments:

Post a Comment