Skip to content

Лабораторная работа №1(обновленная) и 2 #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
31 changes: 31 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.tn.courses.vbuvshov.v2.task1;
import java.util.ArrayList;
import java.util.Arrays;
public class subtask_1
{
public static void main(String[] args)
{
int arr[] = gArr(0, 4, 0, 7, 0, 32, 1, 6, 0);

for (int anArr : arr) System.out.print(anArr + " ");
}

private static int[] gArr(int ... nums)
{
ArrayList<Integer> Nam = new ArrayList<>();
for (int i = 0; i < nums.length; i++)
{
if (nums[i] == 0) Nam.add(i);
}

int arr[] = new int[Nam.size()];

for (int i = 0; i < Nam.size(); i++) arr[i] = Nam.get(i);

return arr;
}


}


23 changes: 23 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.tn.courses.vbuvshov.v2.task1;
import java.util.Arrays;
public class subtask_2
{
public static void main(String args[])
{
double mas[] = {67.2, 68.05, 65.2, 67, 68.55, 67.45};
double max = 0;
int Num = 0;
for (int i = 0; i < mas.length; i++)
{
if (mas[i] > max)
{
max = mas[i];
Num = i;
}
}

System.out.println("Наилучший курс на данный момент в банке № = ");
System.out.println(Num);
}

}
33 changes: 33 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task1/subtask_3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ru.tn.courses.vbuvshov.v2.task1;
import java.util.Arrays;
public class subtask_3
{
public static void main(String[] args)
{
sortirovka();
}

private static void sortirovka()
{
int[] Mas = {41,32,46,7,53,311,1,85,34,6};
System.out.print("Данный массив = ");
for (int aMas : Mas) System.out.print(aMas + " ");
int count = 0;
System.out.print("счетчик равен ");
System.out.print(count);
for (int i = 0; i < Mas.length - 1; i++)
for (int j = 0; j < Mas.length - i - 1; j++)
if (Mas[j] < Mas[j+1])
{
int temp = Mas[j];
Mas[j] = Mas[j+1];
Mas[j+1] = temp;
count++;
}
System.out.print(" конечный массив = ");
for (int aMas : Mas) System.out.print(aMas + " ");
System.out.print("счетчик равен = ");
System.out.print(count);
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package ru.tn.courses.vbuvshov.v2.task2;
public interface ConvertInterface { String convert();}
16 changes: 16 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task2/S_1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package ru.tn.courses.vbuvshov.v2.task2;
public class S_1 {
public static void main(String[] args) {
TheCar car = new TheCarText(0L, TheCar.Type.PassengerCar, "LADA", "Новый автомобиль LADA в наличии.");
print(car);
car = new TheCarJson(1L, TheCar.Type.Truck, "ShockWave 36", "Новый грузовик ShockWave 36 в наличии.");
print(car);
car = new TheCarXml(2L, TheCar.Type.Bike, "Kawasaki Z1000", "Новый мотоцикл Kawasaki Z1000 в наличии.");
print(car);
}
private static String convert(ConvertInterface obj) {return obj.convert();}
private static void print(TheCar theCar) {
if (theCar instanceof TheCarText) {System.out.println("Готово");}
System.out.println(convert(theCar));
}
}
27 changes: 27 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCar.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package ru.tn.courses.vbuvshov.v2.task2;

public abstract class TheCar implements ru.tn.courses.vbuvshov.v2.task2.ConvertInterface {
private Long id;
private Type type;
private String name;
private String description;
public TheCar() {}
public TheCar(TheCar theCar) {
this(theCar.getId(), theCar.getType(), theCar.getName(), theCar.getDescription());
}
public TheCar(Long id, Type type, String name, String description) {
this.id = id;
this.type = type;
this.name = name;
this.description = description;
}
public Long getId() {return this.id;}
public Type getType() {return this.type;}
public String getName() {return this.name;}
public String getDescription() {return this.description;}
public TheCar setId(Long id) {this.id = id;return this;}
public TheCar setType(Type type) {this.type = type;return this;}
public TheCar setName(String name) {this.name = name;return this;}
public TheCar setDescription(String description) {this.description = description;return this;}
public enum Type {PassengerCar, Truck, Bike}
}
12 changes: 12 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarJson.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ru.tn.courses.vbuvshov.v2.task2;

public class TheCarJson extends ru.tn.courses.vbuvshov.v2.task2.TheCar {
public TheCarJson(Long id, Type type, String name, String description) {super(id, type, name, description);}
public TheCarJson(ru.tn.courses.vbuvshov.v2.task2.TheCar theCar) {super(theCar);}
@Override
public String convert() {
return new StringBuilder().append("{\n\t\"id\": ").append(getId()).append(",\n")
.append("\t\"type\": \"").append(getType()).append("\",\n").append("\t\"Name\": \"").append(getName()).append("\",\n")
.append("\t\"Description\": \"").append(getDescription()).append("\",\n}").toString();
}
}
11 changes: 11 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarText.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package ru.tn.courses.vbuvshov.v2.task2;
public class TheCarText extends ru.tn.courses.vbuvshov.v2.task2.TheCar {
public TheCarText() {super();}
public TheCarText(ru.tn.courses.vbuvshov.v2.task2.TheCar theCar) {super(theCar);}
public TheCarText(Long id, Type type, String name, String description) {super(id, type, name, description);}
@Override
public String convert() {
return new StringBuilder().append("ID: ").append(getId()).append("\nType: ").append(getType())
.append("\nName: ").append(getName()).append("\nDescription: ").append(getDescription()).toString();
}
}
23 changes: 23 additions & 0 deletions src/main/java/ru/tn/courses/vbuvshov/v2/task2/TheCarXml.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ru.tn.courses.vbuvshov.v2.task2;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import java.io.StringWriter;

@XmlRootElement(name = "the-car")
public class TheCarXml extends ru.tn.courses.vbuvshov.v2.task2.TheCar {
public TheCarXml() {super();}
public TheCarXml(ru.tn.courses.vbuvshov.v2.task2.TheCar theCar) {super(theCar);}
public TheCarXml(Long id, Type type, String name, String description) {super(id, type, name, description);}
@Override
public String convert() {
try {
StringWriter res = new StringWriter();
Marshaller marshaller = JAXBContext.newInstance(TheCarXml.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(this, res);return res.toString();
} catch (JAXBException e) {e.printStackTrace();}return null;
}
}