Skip to content

Instantly share code, notes, and snippets.

@ad-m
Last active August 29, 2015 14:16
Show Gist options
  • Save ad-m/dd21476ca621039c46a6 to your computer and use it in GitHub Desktop.
Save ad-m/dd21476ca621039c46a6 to your computer and use it in GitHub Desktop.
public class ArrayStack<T> implements Stack<T> {
public Object[] mData = new Object[10];
public int mPosition;
public ArrayStack() {
}
/* (non-Javadoc)
* @see Stack#push(T)
*/
@Override
public void push(T item) {
mData[mPosition++] = item;
}
/* (non-Javadoc)
* @see Stack#pull()
*/
@Override
@SuppressWarnings("unchecked")
public T pull() {
return (T) mData[mPosition--];
}
public static void main(String[] args) {
Stack<String> stack = new ArrayStack<>();
}
import java.util.ArrayList;
/* Napisz klasę opisującą równanie kwadratowe o postaci y = ax 2 + bx + c. Współczynniki a, b i c powinny
byd prywatne. Zdefiniuj następujące publiczne metody:
- konstruktor z parametrami a, b i c nadający wartości współczynnikom,
- obliczająca y dla podanego x,
- wyznaczającą pierwiastki. */
public class Equaution {
private double a;
private double b;
private double c;
public Equaution(double v_a, double v_b, double v_c){
a = v_a;
b = v_b;
c = v_c;
}
public void setA(double a) {
this.a = a;
}
public void setB(double b) {
this.b = b;
}
public void setC(double c) {
this.c = c;
}
public double calc_y(int x){
return a*a+b*x+c;
}
public double discriminant(){
return b*b-4*a*c;
}
public int root_count(){
double d = discriminant();
if(d > 0) return 2;
else if(d == 0) return 1;
else return 0;
}
public ArrayList<Double> root(){
ArrayList<Double> r = new ArrayList<Double>();
if(root_count() == 1) r.add((-b / (a*2)));
if(root_count() == 2) {
r.add(-b + Math.sqrt(discriminant()) / (a*2));
r.add(-b - Math.sqrt(discriminant()) / (2*a));
};
return r;
}
public static void main(String argv[]){
Equaution eq = new Equaution(2.0,3.0,-1.0);
ArrayList<Double> roots = eq.root();
for(int i=0; i<roots.size(); i++){
System.out.print(roots.get(i)+"\n");
}
}
}
import java.util.*;
/* Korzystając z klasy z poprzedniego zadania utwórz tablicę mogącą zapamiętad dane dla 50 osób
(musisz pamiętad ile jest aktualnie zapamiętanych osób w tablicy). Następnie opracuj metody:
A. wpisującą do tablicy obiekt o losowych polach (jak w zadaniu poprzednim);
B. wyświetlającą wszystkie wypełnione elementy tablicy na ekran;
C. wyświetlającą elementy tablicy o nazwisku przekazanym jako parametr;
D. wyświetlającą na ekran osoby, dla których identyfikatory należą do przedziału [a,b];
Następnie napisz program umożliwiający generowania danych dla n osób oraz wyświetlenie ich
danych wg powyższych zasad. */
public class People {
public interface DoThing {
public void doIt(Person p);
}
public interface Filter {
public boolean filter(Person p);
}
public static final DoThing PRINTER = new DoThing() {
@Override
public void doIt(Person p) {
p.wypisz();
}
};
public void printAll() {
each(new Filter() {
@Override
public boolean filter(Person p) {
return true;
}
}, PRINTER);
}
public void printByName(final String name) {
each(new Filter() {
@Override
public boolean filter(Person p) {
return (p.getName().equals(name));
}
}, PRINTER);
}
private ArrayList<Person> lists = new ArrayList<Person>();
public void add(Person p) {
lists.add(p);
}
public void each(Filter f, DoThing thing) {
for (Person item : lists) {
if (f.filter(item)) {
thing.doIt(item);
}
}
}
public static void main(String[] args) {
People data = new People();
for (int i = 0; i < 50; i++) {
data.add(Person.makeRandom());
}
data.printByName("naz-25");
// data.All();
}
}
import java.util.Random;
import java.util.Scanner;
/* Zdefiniuj klasę zawierającą pola danych: nazwisko, rok, miesiąc i dzieo urodzenia
oraz identyfikator numeryczny. Następnie zaimplementuj jej metody:
1. wczytywania danych osoby z klawiatury;
2. inicjującą pola losowymi danymi wg reguł:
 do pola nazwisko wpisz łaocuch 'naz' zakooczony losową liczbą należącą do
przedziału [0,100);
 do pola rok wpisz losową liczbę całkowitą należącą do przedziału [1960, 1985);
 do pola miesiąc wstaw losową liczbę całkowitą należącą do przedziału [1, 13);
 do pola dzieo wstaw losową liczbę całkowitą należącą do przedziału [1, 32);
 do pola id wpisz losową liczbę całkowitą należącą do przedziału [1, 100);
3. wyprowadzającą zawartośd pól danych na ekran;
następnie napisz program umożliwiający wybór losowego generowania lub wprowadzania danych
osoby oraz wyświetlenie jej danych na ekranie.
Uwaga: proszę zapoznad się z klasą Random z pakietu Java.util z dokumentacji JavaDoc. */
public class Person {
private String name;
private int y;
private int m;
private int d;
private int id;
private static final Random random=new Random(1);
public String getName() {
return name;
}
public int getY() {
return y;
}
public int getM() {
return m;
}
public int getD() {
return d;
}
public int getId() {
return id;
}
public void wczytaj() {
Scanner user_input = new Scanner( System.in );
System.out.print("Input name: ");
this.name = user_input.nextLine();
System.out.print("Input day: ");
this.d = user_input.nextInt();
System.out.print("Input month: ");
this.m = user_input.nextInt();
System.out.print("Input year: ");
this.y = user_input.nextInt();
System.out.print("Input ID: ");
this.id = user_input.nextInt();
user_input.close();
}
public void random(){
this.name = "naz"+(random.nextInt()%100);
this.d = random.nextInt()%32+1;
this.m= random.nextInt()%13+1;
this.y = random.nextInt() % (1985-1960) + 1960; // [1960, 1985]
}
public void wypisz(){
System.out.print("name = "+ this.name+"\n");
System.out.print("d = "+ this.d+"\n");
System.out.print("m = "+ this.m+"\n");
System.out.print("y = "+ this.y+"\n");
}
/**
* @param args
*/
public static void main(String[] args) {
Scanner user_input = new Scanner( System.in );
Person p = new Person();
System.out.print("Do you want prompt by hand data?");
if(user_input.nextBoolean()){
p.wczytaj();
}else{
p.random();
}
p.wypisz();
user_input.close();
}
public static Person makeRandom(){
Person p = new Person();
p.random();
return p;
}
}
public class Rational {
private int x,y;
public Rational(int a, int b){
x = a;
y = b;
// NWD
while(a != b){
if(a > b){
a = a - b;
}else{
b = b - a;
}
};
x = x/a;
y = y/a;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public Rational add(Rational arg){
return new Rational(x*arg.getY()+arg.getX()*y, y*arg.getY());
}
public Rational mul(Rational arg){
return new Rational(x*arg.getY()*arg.getX()*y, y*arg.getY());
}
public Rational sub(Rational arg){
return new Rational(x*arg.getY()-arg.getX()*y, y*arg.getY());
}
public Rational div(Rational arg){
return mul(new Rational(arg.getY(), arg.x)); // mnozenie przez odwrotnosc
}
public boolean equals(Rational arg){
return (x*arg.getY() == arg.getX()*y);
}
public int compareTo(Rational arg){
/* Rational tmp = mul(arg);
if(tmp.a/tmp.b > 0){
return 1;
}else if(tmp.a/tmp.b == 0){
return 0;
}else{
return -1;
}*/
int tmp_x = x*arg.getY();
arg.x=arg.getX()*y;
if(tmp_x > arg.getX()){
return 1;
} else if(tmp_x == arg.getX()){
return 0;
};
return -1;
}
public String toString(){
if(y == 1) return String.valueOf(x);
return x + "/" + y;
};
public static void main(String argv[]){
Rational a = new Rational(10,7);
Rational b = new Rational(10, 25);
System.out.print(a.toString() + " + " + b.toString() + " = " + b.add(a).toString()+"\n");
System.out.print(a.toString() + " - " + b.toString() + " = " + b.add(a).toString()+"\n");
System.out.print(a.toString() + " * " + b.toString() + " = " + b.add(a).toString()+"\n");
System.out.print(a.toString() + " / " + b.toString() + " = " + b.add(a).toString()+"\n");
System.out.print(a.toString() + " ?=? " + b.toString() + " = " + b.equals(a)+"\n");
System.out.print(a.toString() + " <?> " + b.toString() + " = " + b.compareTo(a)+"\n");
};
}
/* Napisad klasę Prostokat zawierającą następujące składowe typu int: współrzędne x i y lewego,
dolnego wierzchołka oraz szerokośd i wysokośd prostokąta. Zaimplementowad następujące metody
dla utworzonej klasy: metodę inicjalizującą odpowiednie składowe obiektu,
- metody zwracające wartości odpowiednich składowych,
- metodę wypisującą na ekranie komputera informację o danym prostokącie,
- metodę obliczającą pole powierzchni prostokąta oraz
- metodę, która sprawdzałaby, czy podany punkt o wsp. x i y leży wewnątrz prostokąta
Proszę utworzyd obiekty zaprojektowanej klasy i sprawdzid poprawnośd działania napisanych metod. */
public class Rectangle {
private int x,y, h,w;
public Rectangle(int x, int y, int h, int w) {
super();
this.x = x;
this.y = y;
this.h = h;
this.w = w;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getH() {
return h;
}
public int getW() {
return w;
}
public int surface() {
return h*w;
}
public void print(){
System.out.print("x = " + x + "\n");
System.out.print("y = " + y + "\n");
System.out.print("h = " + h + "\n");
System.out.print("w = " + w + "\n");
}
public boolean contains(int x, int y){
return (this.x < x && this.x+w > x && this.y < y && this.y+h > y );
}
public static void main(String argv[]){
Rectangle a = new Rectangle(3, 3, 15, 10);
a.print();
System.out.print("(5,10) ? " + a.contains(5,10) + "\n");
System.out.print("(0,0) ? " + a.contains(0,0) + "\n");
};
}
public interface Stack<T> {
public abstract void push(T item);
public abstract T pull();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment