Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- static Scanner scan = new Scanner(System.in);
- static public void test1() {
- Vector v = new Vector();
- for (int i = 0; i < 5; i++) {
- v.arr[i] = ((int) (Math.random() * 12) - 15);
- }
- v.size = 5;
- v.print();
- System.out.println("Введите позицию эл значение которого хотите узнать:");
- int N = scan.nextInt();
- System.out.println(v.get(N));
- System.out.println("Введите позицию эл значение которого хотите заменить:");
- int P = scan.nextInt();
- double V = scan.nextInt();
- v.set(V, P);
- v.print();
- System.out.println("Введите позицию эл и значение которое хотите установить:");
- int Po = scan.nextInt();
- double Va = scan.nextInt();
- v.insert(Va, Po);
- v.print();
- v.erase(3);
- v.print();
- v.clear();
- v.print();
- }
- static public void test2() {
- Vector v = new Vector(5);
- v.print();
- System.out.println("Введите позицию эл значение которого хотите узнать:");
- int N = scan.nextInt();
- System.out.println(v.get(N));
- System.out.println("Введите позицию эл значение которого хотите заменить:");
- int P = scan.nextInt();
- double V = scan.nextInt();
- v.set(V, P);
- v.print();
- System.out.println("Введите позицию эл и значение которое хотите установить:");
- int Po = scan.nextInt();
- double Va = scan.nextInt();
- v.insert(Va, Po);
- v.print();
- v.erase(1);
- v.print();
- v.clear();
- v.print();
- }
- static public void test3() {
- double[] arr = new double[10];
- for (int i = 0; i < 5; i++) {
- arr[i] = ((int) (Math.random() * 12) - 15);
- }
- Vector ve = new Vector(arr);
- ve.print();
- System.out.println("Введите позицию эл значение которого хотите узнать:");
- int N = scan.nextInt();
- System.out.println(ve.get(N));
- System.out.println("Введите позицию эл значение которого хотите заменить:");
- int P = scan.nextInt();
- double V = scan.nextInt();
- ve.set(V, P);
- ve.print();
- System.out.println("Введите позицию эл и значение которое хотите установить:");
- int Po = scan.nextInt();
- double Va = scan.nextInt();
- ve.insert(Va, Po);
- ve.print();
- ve.erase(1);
- ve.print();
- ve.clear();
- ve.print();
- }
- public static void main(String[] args) {
- System.out.println("Выберите конструктор: \n" +
- "1.По умолчанию\n" +
- "2.С заданным размером\n" +
- "3.С созданным массивом");
- boolean i=true;
- while(i) {
- int a = scan.nextInt();
- switch (a) {
- case 1: test1();break;
- case 2: test2();break;
- case 3: test3();break;
- case 4: i=false;break;
- }
- }
- }
- }
- public class Vector {
- double[] arr = new double[10];
- int size=0;
- Vector(){}
- Vector(int capacity){
- this.size=capacity;
- for (int i=0;i<capacity;i++){
- this.arr[i]=((int)(Math.random() * 12) - 15);
- }
- }
- Vector(double[] arr){
- copy(arr);
- }
- int getSize(){
- return this.size;
- }
- double get(int pos){
- if((pos>this.arr.length)||(pos<0)){
- return 404;
- }
- else{
- return this.arr[pos-1];
- }
- }
- void set(double val, int pos){
- this.arr[pos-1]=val;
- }
- void insert(double val, int pos) {
- if (this.size == this.arr.length) {
- double[] mas = new double[arr.length * 2];
- for (int i = 0; i < arr.length; i++)
- mas[i] = this.arr[i];
- arr=mas;
- System.out.println("Массив увеличен!");
- }
- double tmp=this.arr[pos-1];
- for(int i=this.arr.length-1;i>0;i--){
- this.arr[i]=this.arr[i-1];
- }
- this.arr[pos-1]=tmp;
- this.arr[pos]=val;
- size++;
- }
- void erase(int pos){
- double tmp=this.arr[pos-1];
- for(int i=0;i<this.arr.length-1;i++){
- this.arr[i]=this.arr[i+1];
- }
- this.arr[pos-1]=tmp;
- size--;
- }
- void clear(){
- for(int i=0;i<this.arr.length;i++){
- this.arr[i]=0;
- }
- }
- void print() {
- if (getSize()== 0) {
- System.out.println("Empty");
- } else {
- for (int i = 0; i < this.arr.length; i++) {
- System.out.print(arr[i] + " ");
- }
- System.out.println();
- }
- }
- double[] copy(double[] arr){
- double[] mas=new double[10];
- for(int i=0; i<10;i++){
- mas[i]=arr[i];
- }
- return mas;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement