Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.FileNotFoundException;
- import java.io.PrintWriter;
- import java.util.List;
- import java.util.Scanner;
- import java.util.ArrayList;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
- class DifferentVectorsLengthException extends Exception {
- //private String msg = "Vector are not equals";
- private static final long serialVersionUID = 1L;
- public DifferentVectorsLengthException(String msg) {
- super(msg);
- }
- }
- class Program {
- public static void main(String[] args) {
- Application app = new Application();
- app.runApplication();
- }
- }
- class MyVector{
- private List<Integer> components;
- public MyVector(List<Integer> com)
- {
- components = new ArrayList<Integer>(com);
- }
- public List<Integer> getComponents()
- {
- return Collections.unmodifiableList(components);
- }
- }
- class VectorUtils {
- public static boolean isEquals(MyVector vector1, MyVector vector2) throws DifferentVectorsLengthException {
- if (vector1.getComponents().size() == vector2.getComponents().size())
- return true;
- else
- throw new DifferentVectorsLengthException("Vector are not equals");
- }
- }
- class Application {
- private String v1Str;
- private String v2Str;
- public void runApplication() {
- boolean isEquals = false;
- while (!isEquals) {
- getDataUser();
- MyVector vector1 = new MyVector(convertStringToList(v1Str));
- MyVector vector2 = new MyVector(convertStringToList(v2Str));
- try {
- isEquals = VectorUtils.isEquals(vector1, vector2);
- } catch (DifferentVectorsLengthException e) {
- System.out.println(e);
- }
- }
- saveSum();
- }
- public void saveSum()
- {
- List<Integer> v1Numbers= convertStringToList(v1Str);
- List<Integer> v2Numbers= convertStringToList(v2Str);
- int sum = 0;
- for (int i = 0; i < v1Numbers.size(); i++)
- {
- sum += (v1Numbers.get(i) + v2Numbers.get(i));
- }
- try (PrintWriter out = new PrintWriter("Sum.txt")) {
- out.println(sum);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
- public void getDataUser() {
- Scanner reader = new Scanner(System.in);
- System.out.println("Enter vector one: ");
- v1Str = reader.nextLine();
- System.out.println("Enter vector two: ");
- v2Str = reader.nextLine();
- }
- private List<Integer> convertStringToList(String v) {
- String parts[] = v.split(" ");
- List<Integer> vInt = new ArrayList<Integer>();
- for (String stringValue : parts) {
- try {
- vInt.add(Integer.parseInt(stringValue));
- } catch (NumberFormatException e) {
- }
- }
- return vInt;
- }
- }
Add Comment
Please, Sign In to add comment