Java Vector To determine if the number is happy: 1. Start with the number itself
ID: 661615 • Letter: J
Question
Java Vector
To determine if the number is happy:
1. Start with the number itself
2. Calculate the sum of the square of its digits.
3. If this sum is equal to 1, the number is happy and can be returned.
Otherwise, repeat step 2 using the new number that was just calculated.
4. If the sequence resulting from step 3 ends in 1, the number is happy.
If there is a cycle, e.g. 4, 16, 37, 58, 89, 145, 42, 20, 4, ..., the number is not happy.
For example:
32 + 22 = 13
12 + 32 = 10
12 + 02 = 1
Therefore the number 32 is happy.
public static boolean isHappy(int number) {
/*
to do
*/
return true;
}
public static Vector happy(int length, int start) {
/*
to do
length 4, start 0 => [1 7 10 13]
length 4, start 1 => [1 7 10 13]
length 4, start 24 => [28 31 32 44]
length 4, start -24 => [28 31 32 44]
*/
return new Vector(length);
}
public Vector cloned() {
/*
to do
[1 1 1 1] => [1 1 1 1]
[1 2 3 4] => [1 2 3 4]
[4 3 2 1] => [4 3 2 1]
*/
return new Vector(length);
}
public Vector shifted(int amount) {
Vector vector = new Vector(length);
/*
to do
[1 2 3 4] 0 => [1 2 3 4]
[1 2 3 4] 1 => [4 1 2 3]
[1 2 3 4] 2 => [3 4 1 2]
[1 2 3 4] 3 => [2 3 4 1]
[1 2 3 4] 4 => [1 2 3 4]
[1 2 3 4] 5 => [4 1 2 3]
*/
return new Vector(length);
}
public Vector scalarAdd(int scalar) {
/*
to do
[1 1 1 1] + 1 => [2 2 2 2]
[1 2 3 4] + 4 => [5 6 7 8]
[2 2 2 2] + -1 => [1 1 1 1]
*/
return new Vector(length);
}
public Vector vectorAdd(Vector other) {
/*
to do
[1 2 3 4] + [0 0 0 0] => [1 2 3 4]
[1 2 3 4] + [4 4 4 4] => [5 6 7 8]
[1 2 3 4] + [1 2 3 4] => [2 4 6 8]
[2 2 2 2] + [-1 -1 -1 -1] => [1 1 1 1]
*/
return new Vector(length);
}
public void display() {
/*
to do
Display each element in the vector seperated by a space.
*/
}
public void displayElement(int index) {
/*
to do
Display the element at the given index.
*/
}
Explanation / Answer
package com.tutai;
import java.util.Vector;
public class HappyNumber {
static Vector<Integer> v=new Vector<Integer>();
public static boolean isHappy(int number) {
Vector<Integer> unique = new Vector<Integer>();
while (unique.add(number))
{
int value = 0;
while (number > 0)
{
value += Math.pow(number % 10, 2);
number /= 10;
}
number = value;
if(number==1||(number+"").length()==1){
break;
}
}
return number == 1;
}
public static Vector happy(int length, int start) {
Vector<Integer> v1=new Vector<Integer>();
for(int i=start;i<=length;i++){
v1.add(v.get(i));
}
return v1;
}
public Vector cloned() {
Vector<Integer> v1=new Vector<Integer>();
for(int i=0;i<v.size();i++){
v1.add(v.get(i));
}
return v1;
}
public Vector shifted(int amount) {
Vector<Integer> v1=new Vector<Integer>();
for(int i=amount;i<v.size();i++){
v1.set(i,v.get(i));
}
for(int i=0;i<amount;i++){
v1.set(i,v.get(i));
}
return v1;
}
public Vector scalarAdd(int scalar) {
Vector<Integer> v1=new Vector<Integer>();
for(int i=0;i<v.size();i++){
Integer sum=Integer.sum(v.get(i),scalar);
v1.set(i, sum);
}
return v1;
}
public Vector vectorAdd(Vector other) {
Vector<Integer> v1=new Vector<Integer>();
for(int i=0;i<v.size();i++){
Integer sum=Integer.sum(v.get(i),(int) other.get(i));
v1.set(i, sum);
}
return v1;
}
public void display() {
for(int i=0;i<v.size();i++){
System.out.println(v.get(i)+" ");
}
}
public void displayElement(int index) {
System.out.println("Element at position "+index+" is: "+v.get(index));
}
public static void main(String[] args){
v.addElement(new Integer(1));
v.addElement(new Integer(2));
v.addElement(new Integer(3));
v.addElement(new Integer(4));
HappyNumber h=new HappyNumber();
if(isHappy(32)){
System.out.println("32 is happy number");
}else{
System.out.println("32 is NOT happy number");
}
if(isHappy(4)){
System.out.println("4 is happy number");
}else{
System.out.println("4 is NOT happy number");
}
h.happy(3, 0);
h.cloned();
h.scalarAdd(1);
h.vectorAdd(v);
h.shifted(2);
h.display();
h.displayElement(3);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.