Java Vector Class
Coding:
Vector.java
// CSE1/4OOF Semester 2 2019 – Progress Check Test
import java.io.*;
import java.util.*;
// Vector class is used to store array of values and number of values
public class Vector
{
protected int[] values;
// constructor of class with integer array of values and default value
public Vector(int[] pValues, int pDefaultValue){
// if integer array is null or if its length is less than 2
if(pValues==null || pValues.length < 2){
// create values array of length 2
values=new int[2];
// copy default value to values
values[0]=values[1]=pDefaultValue;
}
else{
// create values array of length same as integer array
values=new int[pValues.length];
// loop for index from 0 to number of values
for(int index=0;index<pValues.length;index++) {
// copy the value at index from parameter variable to values
values[index]=pValues[index];
}
}
// set size to number of values
size=values.length;
}
// constructor of class with variable number of integer argument
public Vector(int… pValues){
// call first constructor with varargs and default value 0
this(pValues,0);
}
// toString() is used to return integer array of values in the form of string
@Override
public String toString(){
String information=”[ “;
// loop for index from 0 to number of values
for(int index=0;index<size;index++){
// accumulate values at index position to information string
information+= values[index]+” “;
}
information+=”]”;
// return information
return information;
}
// getValues() is used to return a copy of values stored in values array
public int[] getValues(){
// create an integer array of same length as values
int[] pValues = new int[values.length];
// loop for index from 0 to number of values
for(int index=0;index<size;index++) {
// copy the value at index from values to temporary values
pValues[index]=values[index];
}
// return copy of values
return pValues;
}
// getSize() returns size of vector
public int getSize(){
return size;
}
// DO NOT MAKE ANY CHANGE ON main METHOD!!!
public static void main(String[] args) throws IOException
{
Vector v = new Vector();
System.out.println(v);
v = new Vector(0);
System.out.println(v);
v = new Vector(1, 2);
System.out.println(v);
int[] a = {3};
v = new Vector(a, 1);
System.out.println(v);
int[] b = {4, 5, 6};
v = new Vector(b, 0);
System.out.println(v);
b[2] = 7;
System.out.println(v);
b = v.getValues();
b[2] = 7;
System.out.println(v);
}
}
Output:
Coding:
RangedVector.java
// CSE1/4OOF Semester 2 2019 – Progress Check Test
Constructors
import java.io.*;
import java.util.*;
// RangedVector class is the subclass of Vector. Ranged Vector contains values in specific range between lower bound and upper bound
public class RangedVector extends Vector
{
private int lowerbound;
private int upperbound;
// constructor of class with integer array of values, lower bound and upper bound
public RangedVector(int[] pValues, int pLowerbound, int pUpperbound){
// call super class constructor with integer array and lower bound value as default value
super(pValues, pLowerbound);
// set lower and upper bound value
lowerbound=pLowerbound;
upperbound=pUpperbound;
// loop for index from 0 to size of vector
for(int index=0;index<size;index++){
// if the value is less than lower bound
if(values[index]<lowerbound){
// set value at index to lower bound value
values[index]=lowerbound;
}
// if the value is greater than upper bound
else if(values[index]>upperbound){
// set value at index to upper bound value
values[index]=upperbound;
}
}
}
// getDistance() is used to compute the distance between current vector and another ranged vector
public double getDistance(RangedVector anotherRangedVector){
// if another ranged vector is null or if another ranged vector size mismatches with current vector
if(anotherRangedVector==null || anotherRangedVector.getSize()!=size){
// return -1
return -1.0;
}
// set sumOfSqDiff to 0
double sumOfSqDiff=0;
// loop for index from 0 to size of vector
for(int index=0;index<size;index++){
// determine square of their difference and store in sumOfSqDiff
sumOfSqDiff += Math.pow(values[index]-anotherRangedVector.values[index], 2.0);
}
// determine euclidean distance by taking square root of sumOfSqDiff
double euclideanDistance=Math.sqrt(sumOfSqDiff);
// return euclidean distance
return euclideanDistance;
}
// add() is used to add current ranged vector with another ranged vector and return the vector
public Vector add(RangedVector anotherRangedVector){
// if another ranged vector is null or if another ranged vector mismatches the current vector size
if(anotherRangedVector==null || anotherRangedVector.getSize()!=size){
// return null
return null;
}
// create a new array of ranged vector size
int[] addedValues=new int[size];
// loop for index from 0 to size of vector
for(int index=0;index<size;index++){
// add value at index of current vector and another ranged vector and store their sum value
addedValues[index] = values[index]+anotherRangedVector.values[index];
}
// return added vector with added values and default value 0
Vector addedVector=new Vector(addedValues,0);
// return added vector
return addedVector;
}
// toString() is used to return string representation of ranged vector
@Override
public String toString(){
// if both upper bound and lower bound values are same
if(lowerbound == upperbound) {
// return string representation of super class
return super.toString();
}
// form return string representation of super class with lower and upper bound values appended
String information=super.toString() +” in range of [ “+lowerbound+”, “+upperbound+” ]”;
// return the information
return information;
}
// CHANGE ON main METHOD IS ONLY ALLOWED BETWEEN THE TWO COMMENTS INSIDE!!!
public static void main(String[] args) throws IOException
{
if (args == null || args.length != 3)
{
System.out.println(“Error: The program requires as input 3 .dat files.”);
}
else
{
RangedVector[] rv = new RangedVector[3];
//—CHANGE ON main METHOD STARTS HERE
// loop for index from 0 to length of rv
for(int index=0;index<rv.length;index++){
try{
// create file scanner to read input file
Scanner fileScanner=new Scanner(new FileReader(args[index]));
// scan a line from file
String line=fileScanner.nextLine();
// split line by space
String[] information=line.split(” “);
// determine the lower bound and upper bound value
int pLowerbound=Integer.parseInt(information[0]);
int pUpperbound=Integer.parseInt(information[1]);
// create another array to store the remaining values
int[] pValues=new int[information.length-2];
// loop for i from 0 to length of array
for(int i=0;i<pValues.length;i++){
// copy i+2th value from information to pValues after conversion
pValues[i]=Integer.parseInt(information[i+2]);
}
// create ranged vector and store in index position of rv
rv[index]=new RangedVector(pValues,pLowerbound,pUpperbound);
// close the file scanner
fileScanner.close();
}
catch(NumberFormatException ex) {
// display error message
System.out.println(args[index]+” contains invalid data”);
return;
}
catch(FileNotFoundException ex){
// display error message
System.out.println(args[index]+” file is not found”);
return;
}
}
//—CHANGE ON main METHOD ENDS HERE
for (int i = 0; i < 3; i++)
{
System.out.printf(“RV %d: %sn”, i, rv[i]);
}
System.out.println(“—>”);
for (int i = 0; i < 3; i++)
{
for (int j = i + 1; j < 3; j++)
{
System.out.printf(“Euclidean distance between RV %d and RV %d: “, i, j);
System.out.printf(“%.2fn”, rv[i].getDistance(rv[j]));
System.out.printf(“Addition of RV %d and RV %d: “, i, j);
Vector v = rv[i].add(rv[j]);
if (v != null)
{
System.out.println(v);
}
else
{
System.out.println(“Invalid!”);
}
}
}
}
}
}
Output:
References:
Fain, Y. (2015). Java Programming: 24-Hour Trainer. Indianapolis, Indiana: Wiley Publishing Inc.
Schildt, H. (2017). Java 2: The Complete Reference. Boston, Massachusetts: McGraw Hill Professional.