Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions Lab-1.08/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
### IntelliJ IDEA ###
out/
!**/src/main/**/out/
!**/src/test/**/out/

### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
bin/
!**/src/main/**/bin/
!**/src/test/**/bin/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

### VS Code ###
.vscode/

### Mac OS ###
.DS_Store
3 changes: 3 additions & 0 deletions Lab-1.08/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Lab-1.08/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions Lab-1.08/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Lab-1.08/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions Lab-1.08/Lab-1.08.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
209 changes: 209 additions & 0 deletions Lab-1.08/src/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

class BigDecimalOperations {

public static double roundToHundredth(BigDecimal number) {
return number.setScale(2, RoundingMode.HALF_UP).doubleValue();
}

public static double invertSignAndRound(BigDecimal number) {
BigDecimal inverted = number.negate();
return inverted.setScale(1, RoundingMode.HALF_UP).doubleValue();
}
}

abstract class Car {
protected String vinNumber;
protected String make;
protected String model;
protected int mileage;

public Car(String vinNumber, String make, String model, int mileage) {
this.vinNumber = vinNumber;
this.make = make;
this.model = model;
this.mileage = mileage;
}

public abstract String getInfo();
}

class Sedan extends Car {
public Sedan(String vinNumber, String make, String model, int mileage) {
super(vinNumber, make, model, mileage);
}

@Override
public String getInfo() {
return String.format("Sedan [VIN: %s, Make: %s, Model: %s, Mileage: %d]", vinNumber, make, model, mileage);
}
}

class UtilityVehicle extends Car {
private boolean fourWheelDrive;

public UtilityVehicle(String vinNumber, String make, String model, int mileage, boolean fourWheelDrive) {
super(vinNumber, make, model, mileage);
this.fourWheelDrive = fourWheelDrive;
}

@Override
public String getInfo() {
return String.format("UtilityVehicle [VIN: %s, Make: %s, Model: %s, Mileage: %d, FourWheelDrive: %b]", vinNumber, make, model, mileage, fourWheelDrive);
}
}

class Truck extends Car {
private double towingCapacity;

public Truck(String vinNumber, String make, String model, int mileage, double towingCapacity) {
super(vinNumber, make, model, mileage);
this.towingCapacity = towingCapacity;
}

@Override
public String getInfo() {
return String.format("Truck [VIN: %s, Make: %s, Model: %s, Mileage: %d, TowingCapacity: %.2f]", vinNumber, make, model, mileage, towingCapacity);
}
}

abstract class Video {
protected String title;
protected int duration;

public Video(String title, int duration) {
this.title = title;
this.duration = duration;
}

public abstract String getInfo();
}

class TvSeries extends Video {
private int episodes;

public TvSeries(String title, int duration, int episodes) {
super(title, duration);
this.episodes = episodes;
}

@Override
public String getInfo() {
return String.format("TvSeries [Title: %s, Duration: %d minutes, Episodes: %d]", title, duration, episodes);
}
}

class Movie extends Video {
private double rating;

public Movie(String title, int duration, double rating) {
super(title, duration);
this.rating = rating;
}

@Override
public String getInfo() {
return String.format("Movie [Title: %s, Duration: %d minutes, Rating: %.1f]", title, duration, rating);
}
}

interface IntList {
void add(int number);
int get(int id);
}

class IntArrayList implements IntList {
private int[] array;
private int size;

public IntArrayList() {
array = new int[10];
size = 0;
}

@Override
public void add(int number) {
if (size == array.length) {
int newSize = (int) (array.length * 1.5);
int[] newArray = new int[newSize];
System.arraycopy(array, 0, newArray, 0, array.length);
array = newArray;
}
array[size++] = number;
}

@Override
public int get(int id) {
if (id < 0 || id >= size) {
throw new IndexOutOfBoundsException();
}
return array[id];
}
}

class IntVector implements IntList {
private List<Integer> list;

public IntVector() {
list = new ArrayList<>(20);
}

@Override
public void add(int number) {
list.add(number);
if (list.size() == list.size()) {
int newSize = list.size() * 2;
List<Integer> newList = new ArrayList<>(newSize);
newList.addAll(list);
list = newList;
}
}

@Override
public int get(int id) {
if (id < 0 || id >= list.size()) {
throw new IndexOutOfBoundsException();
}
return list.get(id);
}
}

public class Main {
public static void main(String[] args) {
BigDecimal num = new BigDecimal("4.2545");
System.out.println("Round to Hundredth: " + BigDecimalOperations.roundToHundredth(num));

num = new BigDecimal("1.2345");
System.out.println("Invert Sign and Round: " + BigDecimalOperations.invertSignAndRound(num));

Car sedan = new Sedan("1HGBH41JXMN109186", "Honda", "Civic", 12000);
Car utilityVehicle = new UtilityVehicle("1FTRW08L96KC85929", "Ford", "Explorer", 45000, true);
Car truck = new Truck("1GT12UEY1EZ351938", "Chevrolet", "Silverado", 80000, 9500);

System.out.println(sedan.getInfo());
System.out.println(utilityVehicle.getInfo());
System.out.println(truck.getInfo());

Video tvSeries = new TvSeries("Breaking Bad", 45, 62);
Video movie = new Movie("Inception", 148, 8.8);

System.out.println(tvSeries.getInfo());
System.out.println(movie.getInfo());

IntList arrayList = new IntArrayList();
IntList vectorList = new IntVector();

for (int i = 0; i < 12; i++) {
arrayList.add(i);
vectorList.add(i);
}

for (int i = 0; i < 12; i++) {
System.out.println("ArrayList get(" + i + "): " + arrayList.get(i));
System.out.println("VectorList get(" + i + "): " + vectorList.get(i));
}
}
}