Skip to content

Instantly share code, notes, and snippets.

@ad-m
Created March 29, 2015 14:30
Show Gist options
  • Select an option

  • Save ad-m/f6cf549ef1c042dc7014 to your computer and use it in GitHub Desktop.

Select an option

Save ad-m/f6cf549ef1c042dc7014 to your computer and use it in GitHub Desktop.
import java.util.Scanner;
/* 15:54
* Zadanie 5
Napisz program, który pobiera od użytkownika pięd dat
w formie DD.MM.YYYY, następnie segreguje je w kolejności
rosnącej. Jeżeli wpisania przez użytkownika data dotyczy
roku 2013 to program wyrzuca wyjątek.
*/
public class Date {
private int day;
private int month;
private int year;
public Date(int day, int month, int year) {
super();
this.day = day;
this.month = month;
this.year = year;
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public static Date from_string(String str){
String[] part = str.split("\\.", 3);
int day = Integer.parseInt(part[0]);
int month = Integer.parseInt(part[1]);
int year = Integer.parseInt(part[2]);
return new Date(day, month, year);
}
@Override
public String toString() {
return "Date [" + year + "-" + month + "-" + day + "]";
}
public int compareTo(Date obj){
if(obj.getYear() > this.getYear()){
return 1;
}else if(obj.getYear() > this.getYear()){
return -1;
}else{
if(obj.getMonth() > this.getMonth()){
return 1;
}else if(obj.getMonth() > this.getMonth()){
return -1;
}else{
if(obj.getDay() > this.getDay()){
return 1;
}else if(obj.getDay() > this.getDay()){
return -1;
}else{
return 0;
}
}
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
Date[] data = new Date[5];
for(int i=0; i<5; i++){
try{
data[i] = Date.from_string(sc.nextLine());
}catch(NumberFormatException e){
System.out.print("Wrong input data. Try again!");
i--;
}
}
Date tmp;
for(int i=0; i<5; i++){
for(int j=0; j<5; j++){
if(data[i].compareTo(data[j]) > 0){
tmp = data[i];
data[i] = data[j];
data[j] = tmp;
}
}
}
for(int i=0; i<5; i++){
System.out.print(data[i]);
};
sc.close();
}
}
import java.util.Scanner;
/* 14:21 - 14:39
* Zadanie 1
Napisz program, który tworzy obiekt klasy Osoba. Klasa posiada
atrybuty: imię, nazwisko, wiek. Program pozwala na uzupełnienie
wartości atrybutów danymi podanymi z klawiatury. W przypadku wpisania
z klawiatury niepoprawnych wartości program „wyrzuca” wyjątek,
który należy obsłużyd w programie poprzez wypisanie komunikatu
o niepoprawnie podanych danych. Wpisywany wiek musi
byd liczbą w zakresie *0,100+. Imię i nazwisko muszą posiadad
minimum 3 znaki.
*/
public class Osoba {
private String imie;
private String nazwisko;
private int wiek;
private final static Scanner sc = new Scanner(System.in);
public Osoba(String imie, String nazwisko, int wiek) {
super();
this.setImie(imie);
this.setNazwisko(nazwisko);
this.setWiek(wiek);
}
public Osoba(String imie, String nazwisko, String wiek){
super();
this.setImie(imie);
this.setNazwisko(nazwisko);
this.setWiek(wiek);
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
if(imie.length() < 3){
throw new IllegalArgumentException("Name have to be longer than 3");
}
this.imie = imie;
}
public String getNazwisko() {
return nazwisko;
}
public void setNazwisko(String nazwisko) {
if(nazwisko.length() < 3){
throw new IllegalArgumentException("Name have to be longer than 3");
}
this.nazwisko = nazwisko;
}
public int getWiek() {
return wiek;
}
public void setWiek(String wiek) {
this.wiek = Integer.parseInt(wiek);
}
public void setWiek(int wiek) {
if(wiek >=0 && wiek <=100){
throw new IllegalArgumentException("Value of age have to be between 0 and 100");
}
this.wiek = wiek;
}
@Override
public String toString() {
return "Osoba [imie=" + imie + ", nazwisko=" + nazwisko + ", wiek="
+ wiek + "]";
}
private static String nextLine(String text){
System.out.print(text);
return sc.nextLine();
}
public static void main(String[] args) {
Osoba data = new Osoba(nextLine("Your first name:"),
nextLine("Your second name:"),
nextLine("Your age:"));
System.out.print(data);
}
}
/* 14:40 - 14:56
* Zadanie 2
Zaimplementuj Stos za pomocą tablicy liczb całkowitych.
Rozmiar tablicy ma byd podany w konstruktorze. Twoja
implementacja powinna rzucad odpowiednie wyjątki
(trzeba je zdefiniowad) przy próbie zdjęcia elementu z
pustego stosu i położenia elementu na przepełniony stos.
*/
public class Stack {
private int[] stack;
private int count=0;
public Stack(int length) {
super();
this.stack = new int[length];
}
public void push(int data) throws StackFullException{
if(this.count+1 > this.stack.length){
throw new StackFullException("Stack are full");
}
this.stack[this.count]=data;
this.count++;
}
public int pop() throws StackEmptyException{
if(this.count <= 0){
throw new StackEmptyException("Stack are empty");
}
count--;
int el = this.stack[count];
return el;
}
public int length(){
return this.count;
}
/*
public static void main(String[] args) {
Stack data = new Stack(5);
try {
data.push(5);
data.push(2);
data.push(3);
while(data.length()>0){
System.out.println(data.pop());
}
data.pop();
} catch (StackEmptyException e) {
System.out.println("Somethings goes wrong!");
e.printStackTrace();
} catch (StackFullException e){
System.out.println("Somethings goes wrong!");
e.printStackTrace();
}
} */
}
public class StackEmptyException extends Exception {
public StackEmptyException() { }
public StackEmptyException(String msg) {
super(msg);
}
}
public class StackFullException extends Exception {
public StackFullException() {
// TODO Auto-generated constructor stub
}
public StackFullException(String message) {
super(message);
// TODO Auto-generated constructor stub
}
}
/* Zadanie 3
Zaimplementuj klasę Wielomian, reprezentującą wielomian
o współczynnikach całkowitych, z odpowiednimi konstruktorami,
operacjami dodawania, odejmowania, mnożenia oraz ładnym
wypisywaniem. Napisz również metodę, które pobierze
wartośd zmiennej X z klawiatury i obliczy wartośd
wielomianu. Jeżeli wartośd ta będzie większa od 100 to
program wyrzuci zdefiniowany w programie wyjątek. */
public class Wielomian {
public int[] coefficients;
public Wielomian(int[] coefficients) {
super();
this.coefficients = coefficients;
}
public int degree() {
return coefficients.length;
}
public Wielomian add(Wielomian input) {
int[] high = (this.degree() > input.degree() ? this.coefficients
: input.coefficients);
int[] low = (this.degree() < input.degree() ? this.coefficients
: input.coefficients);
int[] n_cof = new int[high.length];
for (int i = 0; i < high.length; i++) {
if (i >= low.length) {
n_cof[i] = high[i];
} else {
n_cof[i] = high[i] + low[i];
}
}
return new Wielomian(n_cof);
}
public Wielomian sub(Wielomian input) {
int[] high = (this.degree() > input.degree() ? this.coefficients
: input.coefficients);
int[] low = (this.degree() < input.degree() ? this.coefficients
: input.coefficients);
int[] n_cof = new int[high.length];
for (int i = 0; i < high.length; i++) {
if (i >= low.length) {
n_cof[i] = high[i];
} else {
n_cof[i] = high[i] - low[i];
}
}
return new Wielomian(n_cof);
}
public Wielomian mul(Wielomian input) { // TODO: Incorrect
int[] high = (this.degree() > input.degree() ? this.coefficients
: input.coefficients);
int[] low = (this.degree() < input.degree() ? this.coefficients
: input.coefficients);
int[] n_cof = new int[high.length];
for (int i = 0; i < high.length; i++) {
if (i >= low.length) {
n_cof[i] = high[i];
} else {
n_cof[i] = high[i] * low[i];
}
}
return new Wielomian(n_cof);
}
public int calculateY(int x) {
int result = 0;
for (int i = 0; i < degree(); i++) {
result += coefficients[i] ^ i;
}
return result;
}
public String toString() {
StringBuilder str = new StringBuilder();
for (int i = 0; i < degree(); i++) {
if (coefficients[i] != 0) {
if (i != 0) {
str.append("+");
}
str.append(coefficients[degree() - i - 1]);
str.append("^");
str.append("(");
str.append(degree() - i);
str.append(")");
}
}
return str.toString();
}
public static void main(String argv[]) {
int[] cof = { 1, 5, 3, 4 };
Wielomian polynomial = new Wielomian(cof);
int[] cof_2 = { 0, 2, 4 };
Wielomian polynomial_2 = new Wielomian(cof_2);
System.out.println(polynomial);
System.out.println(polynomial.sub(polynomial_2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment