Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package zadanie3;
- /**
- *
- * @author Student
- */
- public class Main {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- try{
- Ulamek u = new Ulamek(2,6);
- Ulamek b = new Ulamek(1,6);
- u.addUlamek(b);
- u.downUlamek();
- System.out.println("Wynik = " + u.getLicznik() + "/" + u.getMianownik());
- System.out.println(b);
- }catch (Exception e){
- System.out.println(e.getMessage());
- }
- }
- }
- //------------------------------------------------
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package zadanie3;
- /**
- *
- * @author Student
- */
- public class Ulamek {
- private int licznik;
- private int mianownik;
- public Ulamek(int licznik, int mianownik) throws Exception{
- this.licznik = licznik;
- this.mianownik = mianownik;
- if(mianownik == 0)
- throw new Exception("Wartosc 0 niedozwolona w mianowniku");
- }
- public void downUlamek() throws Exception{
- try{
- Nwd n = new Nwd(this.licznik, this.mianownik);
- int x = n.getNwd();
- this.licznik /= x;
- this.mianownik /= x;
- }catch (Exception e){
- throw new Exception("NWD problem",e);
- }
- }
- public void addUlamek(Ulamek b) throws Exception{
- if(mianownik == b.mianownik)
- this.licznik += b.licznik;
- else if(mianownik != b.mianownik){
- int x = this.mianownik;
- this.licznik *= b.mianownik;
- this.mianownik *= b.mianownik;
- b.mianownik *= x;
- this.licznik += b.mianownik;
- }
- else
- throw new Exception("Cos Sie ... zepsulo");
- }
- public void subUlamek(Ulamek b) throws Exception{
- if(mianownik == b.mianownik)
- this.licznik -= b.licznik;
- else if(mianownik != b.mianownik){
- int x = this.mianownik;
- this.licznik *= b.mianownik;
- this.mianownik *= b.mianownik;
- b.mianownik *= x;
- this.licznik -= b.mianownik;
- }
- else
- throw new Exception("Cos Sie ... zepsulo");
- }
- public int getLicznik() {
- return licznik;
- }
- public void setLicznik(int licznik) {
- this.licznik = licznik;
- }
- public int getMianownik() {
- return mianownik;
- }
- public void setMianownik(int mianownik) {
- this.mianownik = mianownik;
- }
- @Override
- public String toString() {
- return licznik + "/" + mianownik;
- }
- }
- //---------------------------------
- //NWD macie na moim paste
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement