Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java help, I need help
New Posts  All Forums:Forum Nav:

Java help, I need help

post #1 of 3
Thread Starter 
I have the object in a sort of a cascade effect, so i only have to wright a dot operator once.
it testes numbers to find if they are a Lychrel numbers until a specified iteration but, It is not working. It should get a number add it to the inverse of that number and check if it is a palindrome.

example
original number = 90
90+09=99, not a Lychrel number

original number = 72
73+37=110
110+011+121, not a Lychrel number

It is split into a main class named numrunner.
And a object class named cumcheck.

Code:
public class numrunner {

    public static void main ( String [] args)
    {
                numcheck one = new numcheck( 196 , 40 );
                System.out.println(one.toString());
    }}



public class numcheck  {

        private int Nub;
        private int Reva;

        public numcheck (int a , int b)
    {
    Nub = a;
    Reva = b;
    }

    public int setResult()
    {
                int yes = Reva;
                int win = Reva;
                int another = Nub + Switch( Nub );
                int a = another;
                int forever = 0;
                while ( win >0 )
                {
                                int jk = a + Switch(a);
                                forever = jk;
                                win = win - 1;
                                a = jk;
                }
                return forever;
    }

    public String getResult()
    {
        String c ="" + setResult();
        return c;
    }

    public int Switch( int alpha )
    {
                int number = alpha;
                int hello = 0;
                int temp = 0;

                while(number > 0){
                        temp = number%10;
                        hello = herro * 10 + temp;
                        number = number/10;
                }
                        return herro;
        }

    public boolean IfPalindome()
    {
                        int left = 0;
                        int right = getResult().length() -1;

                        while (left < right) {
                                        if (getResult().charAt(left) != getResult().charAt(right)) {
                                                return false;
                        }
                                left++;
                                right--;
                        }
                return true;
    }

    public String toString()
    {
        boolean isValid = IfPalindome();

                String c = null;

        if( isValid ==  false)
                c = "Not a lycherl number";

        if( isValid == true)
                c = "The number "+Nub+" is a real Lyceral at this number of Interations "+ Reva;

                return c;
        }}
The HAFlhaf
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5-2500k @ 4.5 Gigabyte Z68X-UD4-b3 Gigabyte 7950 windforce patriot 8 gb (4x2)1600 cl 8 
OSMonitorPowerCase
Windows 7 64 sp1 Korean Pcbank 2560*1440 toughpower xt 875 Watt HAF 922 
  hide details  
Reply
The HAFlhaf
(13 items)
 
  
CPUMotherboardGraphicsRAM
i5-2500k @ 4.5 Gigabyte Z68X-UD4-b3 Gigabyte 7950 windforce patriot 8 gb (4x2)1600 cl 8 
OSMonitorPowerCase
Windows 7 64 sp1 Korean Pcbank 2560*1440 toughpower xt 875 Watt HAF 922 
  hide details  
Reply
post #2 of 3
Please use the Code tag for your source code
    
CPUMotherboardGraphicsRAM
Intel Core i5-760 @ 3.8GHz ASRock P55 Extreme4 XFX Radeon RX 570 4GB G.SKILL Ripjaws X 8GB DDR3-1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro 256GB SSD Samsung Spinpoint F3 500GB HDD Lite-On DVD-RW Jonathan Taylor Thomas 
OSMonitorMonitorKeyboard
Windows 10 Pro Acer KG240 Acer KG240 Razer BlackWidow Tournament Edition 
PowerCaseMouseMouse Pad
Corsair HX750 Antec Three Hundred Nixeus Revel Nixeus Type-R 
AudioOther
ASUS Xonar DG DXRacer Formula Series 
  hide details  
Reply
    
CPUMotherboardGraphicsRAM
Intel Core i5-760 @ 3.8GHz ASRock P55 Extreme4 XFX Radeon RX 570 4GB G.SKILL Ripjaws X 8GB DDR3-1600 
Hard DriveHard DriveOptical DriveCooling
Samsung 840 Pro 256GB SSD Samsung Spinpoint F3 500GB HDD Lite-On DVD-RW Jonathan Taylor Thomas 
OSMonitorMonitorKeyboard
Windows 10 Pro Acer KG240 Acer KG240 Razer BlackWidow Tournament Edition 
PowerCaseMouseMouse Pad
Corsair HX750 Antec Three Hundred Nixeus Revel Nixeus Type-R 
AudioOther
ASUS Xonar DG DXRacer Formula Series 
  hide details  
Reply
post #3 of 3
Please comment your code properly or use meaningful names for variables so that it is easy to keep track of what your doing in the program.
It took me a while to really get what you were trying to do.

So you're basically repeatedly reversing a number, adding it to the original number and checking whether the answer is a palindrome, for a fixed number of iterations. Have I understood it correctly?

Here's my take on this:
Code (Click to show)
Code:
public class NumRunner{

        private int number;
        private int iterations;

        public NumRunner(int x, int y){
                number = x;
                iterations = y;         
        }
        
        public static void main ( String [] args){
                numcheck one = new numcheck( 196 , 40 );
                System.out.println(one.run());
        }

        public String run(){
                
                int numtotest = number;
                boolean checkpalindrome;
                for (int i = 0; i < iterations; i++){
                        int reversedNumber = switch(numtotest); // Assuming your switch() method works correctly.
                        int sum  = reversedNumber + numtotest;
                        checkpalindrome = ifPalindrome(sum); // Assuming you have an ifPalindrome(para) method that takes a parameter and checks if the int is a palindrome.
                        if (checkpalindrome){
                                return "Not a Lychrel number.";
                                break;
                        }
                        else{
                                numtotest = sum;
                                if (i == iterations - 1){
                                        String toReturn = "The number " + number + " is a Lychrel number.";
                                        return toReturn;

                                }
                        }
                }
        }


}

I have put it all in just one class for my convenience but you obviously may adapt it to your needs. Basically everything can be done in just one for loop in the run() method provided you have the switch(para) and ifPalindrome(para) methods that work correctly. (I have not included them in my code because I think I can safely assume that you know how to code them).

The program returns the string "Not a Lychrel number" and breaks, if it finds the number in question to be a palindrome. Else, it keeps on reversing and adding it to the original, and checking if the sum is a palindrome, for the set number of iterations. If it reaches the last iteration and the sum is still not a palindrome, then it says that the number is a Lychrel number.

I hope this is what you were looking for. I have not checked this code myself to see if it runs correctly but I assume it does.
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java help, I need help