Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Zadacii

Download as pdf or txt
Download as pdf or txt
You are on page 1of 20

FAKULTET INŽENJERSKIH NAUKA

UNIVERZITETA U KRAGUJEVCU

BRTSI4100 – OBJEKTNO ORIJENTISANO


PROGRAMIRANJE

Lekcija 6: Nasleđivanje - Zadaci za vežbe


Nasleđivanje Velibor Isailović

Zadatak 1

Napisati klasu koja reprezentuje odobu. Podatke o svakoj osobi sačinjavaju ime, datum rođenja i
adresa stanovanja.
Napisati klasu koja reprezentuje đaka. Djak je osoba čiji su dodatni podaci škola koju pohađa i razred.
Napisati klasu za radnika. Radnik je osoba za koju se zna naziv firme u kojoj radi i naziv odeljenja.
Klase treba da obezbede metode za učitavanje podataka za svaki objekat klase, a takođe i metodu
toString, za štampanje podataka o objektu.

Klasa Osoba (Person)

package persons;

import java.util.Scanner;

public class Person {


private String name;
private String surname;
private String dateOfBirth;
private String address;

public void read() {


Scanner scanner = new Scanner(System.in);
System.out.println("Name ? : ");
name = scanner.nextLine();
System.out.println("Surname ? : ");
surname = scanner.nextLine();
System.out.println("dateOfBirth ? : ");
dateOfBirth = scanner.nextLine();
System.out.println("Address ? : ");
address = scanner.nextLine();
}

@Override
public String toString() {
return ("Name, surname : " + name + " " + surname + "\n" +
"Address : " + address + "\n" +
"Date of Birth : " + dateOfBirth + "\n");
}
}
Nasleđivanje Velibor Isailović

Klasa Đak (Pupil)

package persons;

import java.util.Scanner;

public class Pupil extends Person {


private String school;
private String schoolClass;

public void read() {


super.read();
Scanner scanner = new Scanner(System.in);
System.out.println("School ? : ");
school = scanner.nextLine();
System.out.println("Class ? : ");
schoolClass = scanner.nextLine();
}

@Override
public String toString() {
return super.toString() +
"School : " + school + "\n" +
"Class : " + schoolClass + "\n";
}
}

Klasa Zaposleni (Employee)

package persons;

import java.util.Scanner;

public class Employee extends Person {


private String company;
private String sector;

public void read() {


super.read();
Scanner scanner = new Scanner(System.in);
System.out.println("Company ? : ");
company = scanner.nextLine();
System.out.println("Sector ? : ");
sector = scanner.nextLine();
}

@Override
public String toString() {
return super.toString() +
"Company : " + company + "\n" +
"Sector : " + sector + "\n";
}
}
Nasleđivanje Velibor Isailović

Glavna klasa

import persons.Employee;
import persons.Person;
import persons.Pupil;
import java.util.Scanner;

public class Zadatak1 {


public static void main(String[] varg) {
Scanner scanner = new Scanner(System.in);
System.out.println("Broj osoba?");
int numberOfPersons;
numberOfPersons = scanner.nextInt();

Person persons[] = new Person[numberOfPersons];

for (int i = 0; i < persons.length; i++) {


System.out.println("Izaberi opciju: O - osoba, D - djak, R - radnik?");
String str = scanner.next();

switch (str.charAt(0)) {
case 'O' : case 'o':
persons[i] = new Person();
persons[i].read();
break;
case 'D' : case 'd':
persons[i] = new Pupil();
persons[i].read();
break;
case 'R' : case 'r':
persons[i] = new Employee();
persons[i].read();
break;
default:
System.out.println("Greska! Ponovi unos.");
i--;
break;
}
}

for (int i = 0; i < persons.length; i++) System.out.print(persons[i]);


}
}
Nasleđivanje Velibor Isailović

Zadatak 2

Napisati apstraktnu klasu koja reprezentuje vozilo. Vozilo sadrži podatak o sopstvenoj težini. Iz klase
Vozilo izvesti klasu Teretno vozilo, koje sadrži podatak o teretu kojim je natovareno. Takođe, iz klase
vozilo izvesti klasu putničko vozilo koje sadrži podatak o prosečnoj težini putnika i ukupnom broju
putnika.
Klase treba da obezbede metode za unificiranu obradu podataka o različitim tipovima vozila.
Predvideti: inicijalizaciju, izračunavanje ukupne težine vozila, metodu za tekstualni opis vozila, metodu
koja vraća tip vozila (jednoslovnu oznaku).
Napisati program koji sa glavnog ulaza učitava podatke o određenom broju vozila a zatim štampa
podatke o vozilima koja mogu da pređu preko mosta čija se nosivost takođe učitava.

Klasa Vozilo (Vehicle)

package vehicles;

public abstract class Vehicle {


private double weight;

public Vehicle(double weight) {


this.weight = weight;
}
public abstract char type();

public double weight() {


return weight;
}
@Override
public String toString() {
return type() + "(" + weight + ",";
}
}

Klasa Teretno vozilo (Truck)

package vehicles;

public class Truck extends Vehicle {


private double load;

public Truck(double weight, double load) {


super(weight);
this.load = load;
}
@Override
public char type() {
return 'T';
}
@Override
public double weight() {
return super.weight() + load;
}
@Override
public String toString() {
return super.toString() + load + ")";
}
}
Nasleđivanje Velibor Isailović

Klasa Putničko vozilo (PassengerVehicle)

package vehicles;

public class PassengerVehicle extends Vehicle {


private double aweragePassengerWeight;
private int numberOfPassengers;

public PassengerVehicle(double weight, double aweragePassengerWeight, int


numberOfPassengers) {
super(weight);
this.aweragePassengerWeight = aweragePassengerWeight;
this.numberOfPassengers = numberOfPassengers;
}

@Override
public char type() {
return 'P';
}
@Override
public double weight() {
return super.weight() + aweragePassengerWeight * numberOfPassengers;
}
@Override
public String toString() {
return super.toString() + aweragePassengerWeight + "," + numberOfPassengers
+ ")";
}
}
Nasleđivanje Velibor Isailović

Glavna klasa

import zadatak2.Vehicle;
import zadatak2.PassengerVehicle;
import zadatak2.Truck;

import java.util.Scanner;

public class Zadatak2 {


public static void main(String[] varg) {
Scanner scanner = new Scanner(System.in);
System.out.println("Broj vozila?");
int numberOfVehicles;
numberOfVehicles = scanner.nextInt();

Vehicle vehicles[] = new Vehicle[numberOfVehicles];

for (int i = 0; i < vehicles.length; i++) {


System.out.println("Vrsta vozila: Teretno - T, putnicko - P? ");
String str = scanner.next();
switch (str.charAt(0)) {
case 'T': case 't':
System.out.println("Sopstvena tezina:");
double weight = scanner.nextDouble();
System.out.println("Teret:");
double load = scanner.nextDouble();
vehicles[i] = new Truck(weight, load);
break;
case 'P': case 'p':
System.out.println("Sopstvena tezina:");
weight = scanner.nextDouble();
System.out.println("Prosecna tezina putnika:");
double aweragePassengerWeight = scanner.nextDouble();
System.out.println("Broj putnika:");
int numberOfPassengers = scanner.nextInt();
vehicles[i] = new PassengerVehicle(weight,
aweragePassengerWeight, numberOfPassengers);
break;
default:
System.out.println("Greska! Ponovi unos.");
i--;
break;
}
}
System.out.println("Nosivost mosta: ");
double bridgeLoadCapacity = scanner.nextDouble();

System.out.println("Vozila koja mogu da predju preko mosta: ");


for (int i = 0; i < vehicles.length; i++) {
if (vehicles[i].weight() < bridgeLoadCapacity) {
System.out.println(vehicles[i] + " - " + vehicles[i].weight());
}
}
}
}
Nasleđivanje Velibor Isailović

Zadatak 3

Napisati apstraktnu klasu koja reprezentuje predmet. Klasa Predmet sadrži podatak o specifičnoj težini
i jednoslovnu oznaku vrste predmeta.
Sfera je predmet definisan poluprečnikom (r) i jednoslovnom oznakom.
Kvadar je predmet definisan dužinama stranica (a, b, c) i jednoslovnom oznakom.
Napisati klase koje omogućavaju unificiranu obradu podataka prethodno opisanih klasa. Predvideti
inicijalizaciju zadatim parametrima, izračunavanje zapremine i težine, učitavanje podataka o
predmetima s glavnog ulaza, štampanje podataka o predmetima na glavnom izlazu. Odštampati
podatke o masama predmeta koji imaju masu veću od prosečne mase svih predmeta.

Klasa Predmet (Article)

package zadatak3;

import java.util.Scanner;

public abstract class Article {


private double specificWeight;
protected Article(double specificHeight) {
this.specificWeight = specificHeight;
}
protected Article() {
specificWeight = 1;
}
public abstract char type();
public abstract double volume();
public double mass() {
return volume() * specificWeight;
}
public void read() {
System.out.println("Unesi specificnu tezinu: ");
Scanner scanner = new Scanner(System.in);
specificWeight = scanner.nextDouble();
}
@Override
public String toString() {
return type() + "[" + specificWeight + "]";
}
}
Nasleđivanje Velibor Isailović

Klasa Sfera (Sphere)

package zadatak3;

import java.util.Scanner;

public class Sphere extends Article {


public static final char TYPE = 'S';
private double radius;

public Sphere(double specificHeight, double radius) {


super(specificHeight);
this.radius = radius;
}
public Sphere() {
this(1,1);
}
@Override
public char type() {
return TYPE;
}
@Override
public double volume() {
return 4.0/3.0 * Math.pow(radius,3) * Math.PI;
}
@Override
public void read() {
super.read();
System.out.println("Unesi poluprecnik sfere: ");
Scanner scanner = new Scanner(System.in);
radius = scanner.nextDouble();
}
@Override
public String toString() {
return super.toString() + radius + "]";
}
}
Nasleđivanje Velibor Isailović

Klasa Prizma (Prism)

package zadatak3;

import java.util.Scanner;

public class Prism extends Article {


public static final char TYPE = 'P';
private double sideA, sideB, sideC;

public Prism(double specificHeight, double a, double b, double c) {


super(specificHeight);
sideA = a;
sideB = b;
sideC = c;
}
public Prism() {
this(1,1,1,1);
}
@Override
public char type() {
return TYPE;
}
@Override
public double volume() {
return sideA * sideB * sideC;
}

@Override
public void read() {
super.read();
Scanner scanner = new Scanner(System.in);
System.out.println("Ucitaj stranice prizme:");
sideA = scanner.nextDouble();
sideB = scanner.nextDouble();
sideC = scanner.nextDouble();
}

@Override
public String toString() {
return super.toString() + sideA + "," + sideB + "," + sideC + "]";
}
}
Nasleđivanje Velibor Isailović

Glavna klasa

import zadatak3.Article;
import zadatak3.Prism;
import zadatak3.Sphere;

import java.util.Scanner;

public class Zadatak3 {


public static void main(String[] varg) {
Scanner scanner = new Scanner(System.in);
System.out.println("Broj predmeta?");
int numberOfArticles;
numberOfArticles = scanner.nextInt();

Article articles[] = new Article[numberOfArticles];


double mass = 0.0;

for (int i = 0; i < articles.length; i++) {


System.out.println("Vrsta predmeta: S - sfera, P - prizma?");
String str = scanner.next();

switch (str.charAt(0)) {
case 'S' : case 's':
articles[i] = new Sphere();
break;
case 'P' : case 'p':
articles[i] = new Prism();
break;
default:
System.out.println("Greska! Ponovi unos.");
i--;
continue;
}

if (articles[i] != null) articles[i].read();


System.out.println(articles[i] + " (Masa = " + articles[i].mass() +
")");
mass += articles[i].mass();
}

double awerageMass = mass / articles.length;


System.out.println("Prosecna masa predmeta: " + awerageMass);

System.out.println("Predmeti cija je masa veca od prosecne: ");


for (int i = 0; i < articles.length; i++)
if (articles[i].mass() > awerageMass)
System.out.println(articles[i] + "(Masa = " + articles[i].mass() +
")");
}
}
Nasleđivanje Velibor Isailović

Zadatak 4

Napisati apstraktnu klasu za geometrijsku figuru u ravni. Klasa treba da obezbedi sledeće
funkcionalnosti :
• Stvaranje fugure sa težištem u zadatoj tački.
• Premeštanje figure u novu tačku i pomeranje figure za određeni pomak.
• Izračunavanje obima i površine figure.
• Učitavanje figure s glavnog ulaza.
• Štampanje figure pomoću redefinisane metode toString.
Napisati klase za trougao, kvadrat i krug koje nasleđuju apstraktnu klasu figura.

Klasa Tačka (Point)

package zadatak4;

import java.util.Scanner;

public class Point {


private double x;
private double y;

public double getX() { return x; }


public double getY() { return y; }
public void setX(double x) { this.x = x; }
public void setY(double y) { this.y = y; }

public Point(){}
public Point(double x) {
this.x = x;
}
public Point(double x, double y) {
this.x = x;
this.y = y;
}
public void read() {
Scanner scanner = new Scanner(System.in);
System.out.println("Unesi koordinate (x,y):");
x = scanner.nextDouble();
y = scanner.nextDouble();
}

@Override
public String toString() {
return "(" + x + ", " + y + ")";
}

public static final Point ORIGIN = new Point();


}
Nasleđivanje Velibor Isailović

Klasa Figura (Figure)

package zadatak4;

import java.util.Scanner;

public abstract class Figure {


private Point centerOfGravity;

public Figure() {
centerOfGravity = new Point();
}
public Figure(Point centerOfGravity) {
this.centerOfGravity = centerOfGravity;
}
public final void setCenterOfGravity(double x, double y) {
centerOfGravity.setX(x);
centerOfGravity.setY(y);
}
public final void move(double x, double y) {
centerOfGravity.setX(centerOfGravity.getX() + x);
centerOfGravity.setY(centerOfGravity.getY() + y);
}
public abstract double circumference();
public abstract double area();
public void read() {
System.out.println("Teziste: ");
centerOfGravity.read();
}
@Override
public String toString() {
return "Center of gravity: " + centerOfGravity;
}
}

Klasa Kvadrat (Square)

package zadatak4;
import java.util.Scanner;

public class Square extends Figure {


private double side;
public Square() {}
public Square(double side) { this.side = side; }
public Square(double side, Point centerOfGravity) {
super(centerOfGravity);
this.side = side;
}
@Override
public double circumference() { return 4.0 * side; }
@Override
public double area() {return side * side; }
@Override
public void read() {
super.read();
Scanner scanner = new Scanner(System.in);
System.out.println("Stranica kvadrata? :");
side = scanner.nextDouble();
}
@Override
public String toString() {
return "Kvadrat [" + super.toString() + ", stranica = " + side +
", obim = " + circumference() + ", povrsina = " + area() + "]";
}
}
Nasleđivanje Velibor Isailović

Klasa Trougao (Triangle)

package zadatak4;

import java.util.Scanner;

public class Triangle extends Figure {


private double sideA;
private double sideB;
private double sideC;

public Triangle() {}
public Triangle(double sidesABC) {
sideA = sideB = sideC = sidesABC;
}
public Triangle(double sideA, double sidesBC) {
this.sideA = sideA;
sideB = sideC = sidesBC;
}
public Triangle(double sideA, double sideB, double sideC) {
this.sideA = sideA;
this.sideB = sideB;
this.sideC = sideC;
}
@Override
public double circumference() {
return sideA + sideB + sideC;
}
@Override
public double area() {
double s = (sideA + sideB + sideC) / 3;
return Math.sqrt(s * (s - sideA) * (s - sideB) * (s - sideC));
}
@Override
public void read() {
super.read();
Scanner scanner = new Scanner(System.in);
System.out.println("Stranice trougla (a,b,c)? :");
sideA = scanner.nextDouble();
sideB = scanner.nextDouble();
sideC = scanner.nextDouble();
}
@Override
public String toString() {
return "Trougao [" + super.toString() + ", stranica a = " + sideA +
", stranica b = " + sideB + ", stranica c = " + sideC +
", obim = " + circumference() + ", povrsina = " + area() + "]";
}
}
Nasleđivanje Velibor Isailović

Klasa Krug (Circle)

package zadatak4;

import java.util.Scanner;

public class Circle extends Figure {


private double radius;

public Circle() {}
public Circle(double radius) { this.radius = radius; }
public Circle(double radius, Point centerOfGravity) {
super(centerOfGravity);
this.radius = radius;
}

@Override
public double circumference() {
return 2.0 * radius * Math.PI;
}

@Override
public double area() {
return radius * radius * Math.PI;
}

@Override
public void read() {
super.read();
Scanner scanner = new Scanner(System.in);
System.out.println("Poluprecnik kruga? :");
radius = scanner.nextDouble();
}

@Override
public String toString() {
return "Krug [" + super.toString() + ", radius = " + radius +
", obim = " + circumference() + ", povrsina = " + area() + "]";
}
}
Nasleđivanje Velibor Isailović

Glavna klasa

import zadatak4.*;
import java.util.Scanner;

public class Zadatak4 {


public static void main(String[] varg) {
Scanner scanner = new Scanner(System.in);
System.out.println("Broj figura?");
int numberOfFigures;
numberOfFigures = scanner.nextInt();

Figure figures[] = new Figure[numberOfFigures];

for (int i = 0; i < figures.length; i++) {


System.out.println("Vrsta figure? T - trougao, K - kvadrat, C - krug");
String str = scanner.next();

switch (str.charAt(0)) {
case 'T' : case 't':
figures[i] = new Triangle();
break;
case 'K' : case 'k':
figures[i] = new Square();
break;
case 'C' : case 'c':
figures[i] = new Circle();
break;
default:
System.out.println("Greska! Ponovi unos.");
i--;
continue;
}

if (figures[i] != null) figures[i].read();


}

for (Figure f : figures) {


System.out.println(f);
}
}
}
Nasleđivanje Velibor Isailović

Zadatak 5
Napisati klasu Vektor u programskom jeziku Java.
Klasa Vektor je predstavljena komponentama x, y, z koje predstavljaju projekcije vektora na ose
Dekartovog koordinatnog sistema. Klasa treba da obezbedi inicijalizaciju komponenata, izračunavanje
intenziteta vektora, zbir dva vektora, proizvod vektora i skalara, i tekstualni opis klase.
Klasa Brzina je izvedena iz klase Vektor. U klasi Brzina treba redefinisati metodu toString, koja će
štampati tekstualni opis klase u formatu V(x,y,z).
Klasa Materijalna tačka predstavlja pokretnu tačku u prostoru. Sadrži podatke vektor položaja, brzina
i ubrzanje. Tačke se mogu kretati u prostoru pravolinijski, zadatom konstantnom brzinom i
konstantnim ubrzanjem. Materijalna tačka sadrži automatski generisan identifikacioni broj.
U glavnoj klasi napraviti glavnu metodu i u njoj učitati podatke o nekoliko tačaka, zajedno sa podacima
o njihovoj pojedinačnoj brzini i ubrzanju. Štampati podatke o položaju tačke najbliže koordinatnom
početku posle intervala od 1, 2, 3 , 4 i 5 sekundi.

Klasa Vektor (Vector)

package zadatak5;

public class Vector {


private double x, y, z;

public double getX() { return x; }


public double getY() { return y; }
public double getZ() { return z; }
public void setX(double x) { this.x = x; }
public void setY(double y) { this.y = y; }
public void setZ(double z) { this.z = z; }

public Vector() {}
public Vector(double x, double y, double z) {
this.x = x;
this.y = y;
this.z = z;
}
public double intensity() {
return Math.sqrt(Math.pow(x,2) + Math.pow(y,2) + Math.pow(z,2));
}
Vector add(Vector other) {
return new Vector(x + other.x, y + other.y, z + other.z);
}
Vector multiply(double coefficient) {
return new Vector(x * coefficient, y * coefficient, z * coefficient);
}
public double distance(Vector other) {
double dist = Math.sqrt(Math.pow(this.x - other.x, 2) + Math.pow(this.y -
other.y, 2) + Math.pow(this.z - other.z, 2));
return dist;
}

@Override
public String toString() {
return "(" + x + ", "+ y + ", "+ z + ")";
}
}
Nasleđivanje Velibor Isailović

Klasa Brzina (Velocity)

package zadatak5;

public class Velocity extends Vector {


public Velocity() {
super(0,0,0);
}
public Velocity(double x, double y, double z) {
super(x, y, z);
}
@Override
public String toString() {
return "V" + super.toString();
}
}

Klasa Ubrzanje (Acceleration)

package zadatak5;

public class Acceleration extends Vector {


public Acceleration() {
super(0,0,0);
}
public Acceleration(double x, double y, double z) {
super(x, y, z);
}
@Override
public String toString() {
return "a" + super.toString();
}
}
Nasleđivanje Velibor Isailović

Klasa Materijalna tačka (MaterialPoint)

package zadatak5;

public class MaterialPoint {


private static int idGenerator = 0;
private final int ID = idGenerator++;

public void setRadiusVector(Vector radiusVector) {


this.radiusVector = radiusVector;
}

public void setVelocity(Velocity velocity) {


this.velocity = velocity;
}

public void setAcceleration(Acceleration acceleration) {


this.acceleration = acceleration;
}

private Vector radiusVector;


private Velocity velocity;
private Acceleration acceleration;

public MaterialPoint() {
radiusVector = new Vector();
velocity = new Velocity();
acceleration = new Acceleration();

public MaterialPoint(Vector pos, Velocity velocity, Acceleration acceleration){


this.radiusVector = pos;
this.velocity = velocity;
this.acceleration = acceleration;
}
public Vector getPosAtTime(double time) {
Vector pos = new Vector();
pos.add(this.radiusVector);
pos.add(this.velocity.multiply(time));
pos.add(this.acceleration.multiply(time*time / 2.0));
return pos;
}

@Override
public String toString() {
return "Materijalna tacka " + ID + radiusVector;
}
}
Nasleđivanje Velibor Isailović

Glavna klasa

import zadatak5.*;

import java.util.Scanner;

public class Zadatak5 {


public static void main(String[] varg) {
Scanner scanner = new Scanner(System.in);
System.out.println("Broj materijalnih tacaka?");
int numberOfMaterialPoints;
numberOfMaterialPoints = scanner.nextInt();

MaterialPoint materialPoints[] = new MaterialPoint[numberOfMaterialPoints];

for (int i = 0; i < materialPoints.length; i++) {


materialPoints[i] = new MaterialPoint();

System.out.println("Koordinate tacke " + (i + 1) + " :");


double x = scanner.nextDouble();
double y = scanner.nextDouble();
double z = scanner.nextDouble();
materialPoints[i].setRadiusVector(new Vector(x,y,z));

System.out.println("Komponente brzine tacke " + (i + 1) + " :");


x = scanner.nextDouble();
y = scanner.nextDouble();
z = scanner.nextDouble();
materialPoints[i].setVelocity(new Velocity(x,y,z));

System.out.println("Komponente ubrzanja tacke " + (i + 1) + " :");


x = scanner.nextDouble();
y = scanner.nextDouble();
z = scanner.nextDouble();
materialPoints[i].setAcceleration(new Acceleration(x,y,z));
}

Vector positions[] = new Vector[materialPoints.length];


for (int i = 1; i <= 5; i++) {
double time = (double) i;
for (int j = 0; j < materialPoints.length; j++) positions[j] =
materialPoints[j].getPosAtTime(time);

double minDistance = 1.0e+30;


int index = -1;
Vector origin = new Vector(0,0,0);

for (int j = 0; j < positions.length; j++) {


Vector position = positions[j];
if (position.distance(origin) < minDistance) {
minDistance = position.distance(origin);
index = j;
}
}

System.out.println("Tacka najbliza koordinatnom pocetku: " +


materialPoints[index]);
}
}
}

You might also like