NumberList.java (implements the linked list)public class NumberLis.pdf
1. NumberList.java (implements the linked list)
public class NumberList{
Node first;
Node last;
public NumberList(){
first = null;
last = null;
}
public NumberList(Node node){
this.first = node;
this.last = node;
}
public boolean isEmpty(){
return first == null;
}
public void setLast(Node node){
this.last = node;
}
public void insert(Node node){
if (first==null){
this.first = node;
this.last = node;
}
else{
node.previous = this.last;
node.previous.next = node;
node.next = null;
this.last = node;
}
}
public boolean inList(String num){
Node l = first;
while (l.next != null){
if (l.number.equals(num)){
return true;
2. }
else{
l = l.next;
}
}
return false;
}
public void printList(){
Node l = first;
while(l.next!=null){
System.out.print(l.number+" ");
l = l.next;
}
System.out.println();
}
}
Node.java (implements a single node in the linked list; stores number and pointers)
public class Node{
String number;
Node previous;
Node next;
public Node(String num){
number = num;
previous = null;
next = null;
}
public Node(String num, Node p, Node n){
number = num;
previous = p;
next = n;
}
}
Main.java (contains the main method and the helper methods to solve the questions given)
import java.util.Scanner;
import java.util.List;
3. import java.util.ArrayList;
public class Main{
static boolean checkHappiness(String num){
Node current = new Node(num, null, null);
NumberList numberList = new NumberList(current);
int len = num.length();
int resultant = 0;
for (int i=0; i happyNumbersfrom1to10000(){
ArrayList numbers = new ArrayList();
for (int j=0; j<10000; j++){
if (checkHappiness(String.valueOf(j+1))){
numbers.add(String.valueOf(j+1));
}
}
return numbers;
}
static void happyNumbersfrom9001to10000(){
for (int j=9001; j<=10000; j++){
//System.out.println(j);
if (checkHappiness(String.valueOf(j))){
System.out.println(String.valueOf(j));
}
}
}
static String getLargeHappyNumber(){
for (long i=10000000000000000000L; i<1000000000000000000000L; i++){
if (checkHappiness(String.valueOf(i))){
return String.valueOf(i);
}
return "-1";
}
}
static String getLargeUnhappyNumber(){
for (long i=10000000000000000000L; i<1000000000000000000000L; i++){
if (!checkHappiness(String.valueOf(i))){
return String.valueOf(i);
4. }
return "-1";
}
}
public static void main(String[] args){
happyNumbersfrom9001to10000();
System.out.println(happyNumbersfrom1to10000());
}
}
Solution
NumberList.java (implements the linked list)
public class NumberList{
Node first;
Node last;
public NumberList(){
first = null;
last = null;
}
public NumberList(Node node){
this.first = node;
this.last = node;
}
public boolean isEmpty(){
return first == null;
}
public void setLast(Node node){
this.last = node;
}
public void insert(Node node){
if (first==null){
this.first = node;
this.last = node;
}
5. else{
node.previous = this.last;
node.previous.next = node;
node.next = null;
this.last = node;
}
}
public boolean inList(String num){
Node l = first;
while (l.next != null){
if (l.number.equals(num)){
return true;
}
else{
l = l.next;
}
}
return false;
}
public void printList(){
Node l = first;
while(l.next!=null){
System.out.print(l.number+" ");
l = l.next;
}
System.out.println();
}
}
Node.java (implements a single node in the linked list; stores number and pointers)
public class Node{
String number;
Node previous;
Node next;
public Node(String num){
number = num;
previous = null;
6. next = null;
}
public Node(String num, Node p, Node n){
number = num;
previous = p;
next = n;
}
}
Main.java (contains the main method and the helper methods to solve the questions given)
import java.util.Scanner;
import java.util.List;
import java.util.ArrayList;
public class Main{
static boolean checkHappiness(String num){
Node current = new Node(num, null, null);
NumberList numberList = new NumberList(current);
int len = num.length();
int resultant = 0;
for (int i=0; i happyNumbersfrom1to10000(){
ArrayList numbers = new ArrayList();
for (int j=0; j<10000; j++){
if (checkHappiness(String.valueOf(j+1))){
numbers.add(String.valueOf(j+1));
}
}
return numbers;
}
static void happyNumbersfrom9001to10000(){
for (int j=9001; j<=10000; j++){
//System.out.println(j);
if (checkHappiness(String.valueOf(j))){
System.out.println(String.valueOf(j));
}
}
}
7. static String getLargeHappyNumber(){
for (long i=10000000000000000000L; i<1000000000000000000000L; i++){
if (checkHappiness(String.valueOf(i))){
return String.valueOf(i);
}
return "-1";
}
}
static String getLargeUnhappyNumber(){
for (long i=10000000000000000000L; i<1000000000000000000000L; i++){
if (!checkHappiness(String.valueOf(i))){
return String.valueOf(i);
}
return "-1";
}
}
public static void main(String[] args){
happyNumbersfrom9001to10000();
System.out.println(happyNumbersfrom1to10000());
}
}