Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Vector class: import java.util.*; public class MyVector { protected ArrayList vectorObject; public MyVector(double[] intvalues) { vectorObject = new ArrayList(); int j = 0; while (j

image text in transcribedimage text in transcribedimage text in transcribed

Vector class:

import java.util.*;

public class MyVector {

protected ArrayList vectorObject;

public MyVector(double[] intvalues) {

vectorObject = new ArrayList();

int j = 0; while (j

}

public MyVector(MyVector c) {

vectorObject = new ArrayList();

int j = 0; Double element; while (j

}

public int size() { return vectorObject.size(); }

public double get(int index) { return vectorObject.get(index); }

public MyVector plus(MyVector z) {

//create new sum array double[] arraySum = new double[size()];

int j = 0; while (j

arraySum[j] = z.get(j) + this.get(j); ++j; }

return new MyVector(arraySum); }

public MyVector minus(MyVector z) {

//create new difference array double[] arrayDiff = new double[size()];

int j = 0; while (j

arrayDiff[j] = this.get(j) - z.get(j); ++j; }

return new MyVector(arrayDiff); }

public MyVector scaledBy(double multiplier) {

//create new Scalar array double[] arrayScalar = new double[size()];

int j = 0; while (j

return new MyVector(arrayScalar); }

public double dot(MyVector y) {

//initialize dotProduct value double dotProduct = 0;

int j = 0;

while (j

dotProduct += get(j) * y.get(j); ++j; }

return dotProduct; }

public double abs() {

//Square root of x dot x return Math.sqrt(dot(this)); }

@Override public String toString() {

StringBuilder buildString = new StringBuilder("Vector Values = "); buildString.append("{");

int j = 0; while (j

if (j != this.size() - 1) { //if j is not equal to the final index in array String f = String.format("%.02f", get(j)); buildString.append(f); buildString.append(", ");

} else { String g = String.format("%.02f", get(j)); buildString.append(g); } ++j;

} buildString.append("}"); return buildString.toString(); }

@Override public boolean equals(Object o) {

//obtain a reference of a different but matching type MyVector vObject = (MyVector) o;

int index = 0; while (index

if (this.get(index) != vObject.get(index)) { return false; } ++index; } return true;

} }

Driver Vector Class:

import java.util.*;

public class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

System.out.println("Size of vector Array?"); int size = input.nextInt();

double[] xArray = new double[size]; System.out.println("Vector values for x: ");

for (int i = 0; i

double[] yArray = new double[size]; System.out.println("Vector double values for y: ");

for (int j = 0; j

//Create new array objects x and y MyVector y = new MyVector(yArray); MyVector x = new MyVector(xArray);

System.out.println("X " + x.toString()); System.out.println("Y " + y.toString());

//Create MyVector object z. Add yArray to xArray MyVector z = x.plus(y); System.out.println("X + Y " + z.toString());

//Define Vector z, subtract yArray from xArray z = x.minus(y); System.out.println("X - Y " + z.toString());

//Find the scalar-product of x System.out.println("Enter a scalar value for x: "); double multiplier = input.nextDouble(); z = x.scaledBy(multiplier); System.out.println(multiplier + " * X " + z.toString());

// Find the dot product x dot y double dotProduct = x.dot(y); String formatDot = String.format("%.02f", dotProduct); System.out.println("The Dot Product X Y = " + formatDot);

// Find the Absolute value of x double absoluteValue = x.abs(); String formatAV = String.format("%.02f", absoluteValue); System.out.println("Absolute value of X = " + formatAV);

// Check if x equals y if (x.equals(y)) { System.out.println("X and Y are equal."); } else { System.out.println("X and Y and not equal."); } } }

Objective This assignment will help you understand how to use the Linkedlist class and the ListIterator interface in the Java Collections Framework. A palindrome is a word, phrase, number or other sequence of objects that has the property of reading the same in either direction (the adjustment of punctuation and spaces between words is generally permitted). The word "palindrome" comes from the Greek tliv (palin) "back" and Spuos (dromos) "way, direction". For example, the following is a palindrome: ABADABA. In this assignment, we solve the palindrome recognition problem. Program assignment Develop and test a program to recognize if a sequence of MyVector objects (defined in Project 1) is a palindrome, i.e. is the same from right to left as from left to right. . . Design specifications Create a Java project project 4 and a package package4. Design a driver class which will have a method to recognize a palindrome. Your sequence should be input from a file and then stored in a LinkedList object. Use two ListIterator objects, initially positioned one at the beginning, the other at the end of your linked list. One iterator should advance, the other should retreat. The program should print the result: Palindrome or Not palindrome

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Administrator Limited Edition

Authors: Martif Way

1st Edition

B0CGG89N8Z

More Books

Students also viewed these Databases questions

Question

Is my message persuasive? Motivating?

Answered: 1 week ago