Help me tweak this method

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 896 Views - Last Post: 03 March 2013 - 06:37 PM Rate Topic: -----

#16 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Help me tweak this method

Posted 27 February 2013 - 06:15 AM

import java.util.Scanner;


public class OddOut {
static Scanner sc=new Scanner(System.in);
static String num="";
	public OddOut() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		char c;
		StringBuilder build = new StringBuilder("");
		// TODO Auto-generated method stub
System.out.println("Enter the Number ");
num=sc.next();

for(int i=0;i<num.length();i++){
	 c=num.charAt(i);
int j=Integer.parseInt(String.valueOf(c));
;
if(checkEven(j)){
	
	build.append(String.valueOf(j));
}
}
int buint=Integer.parseInt(build.toString());
	 System.out.println("Odds Out :  "+buint);

	}

	private static boolean checkEven(int j) {
		// TODO Auto-generated method stub
		if(j%2==0)
		return true;
		return false;
	}
}

Was This Post Helpful? -1
  • +
  • -

#17 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Help me tweak this method

Posted 27 February 2013 - 06:28 AM

The required approach would be


Spoiler

Was This Post Helpful? -1
  • +
  • -

#18 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help me tweak this method

Posted 27 February 2013 - 02:38 PM

Sorry Burak but... the requirement is to make it recursive

And if you use your iterative version
   System.out.println("Enter the Number ");
   num=sc.next();

   for(int i=0;i<num.length();i++){
	 c=num.charAt(i);
   int j=Integer.parseInt(String.valueOf(c));




no need to make it as character. Simply nextInt() and use % 10 to isolate each digit as a int


	private static boolean checkEven(int j) {
		if(j%2==0)
		return true;
		return false;
	}


why not simply
	private static boolean checkEven(int j) {
		return j%2==0;
	}


Was This Post Helpful? 0
  • +
  • -

#19 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 27 February 2013 - 03:08 PM

View Postbaavgai, on 27 February 2013 - 05:59 AM, said:

View Postoha055, on 26 February 2013 - 12:19 PM, said:

Yes, this was the easiest way to do it


Um, no, it's kind of a hack. It's almost certainly not what an instructor would be looking for. It's about problem solving, not method calling.


Here are the first two lines:
int evenDigits(int n) {
if(n == 0) { return 0; }
int digit = n % 10;
[/code]

You have to ask yourself why I pulled the digit out. The return statement must include a digit or not. It must also have the collection of all prior values.

Rather than just cough up the code, let's have a look at how recursion works:
-call evenDigits(8342116)
--digit = 6
--call evenDigits(834211)
---digit = 1
---call evenDigits(83421)
----digit = 1
----call evenDigits(8342)
-----digit = 2
-----call evenDigits(834)
------digit = 4
------call evenDigits(83)
-------digit = 3
-------call evenDigits(8)
--------digit = 8
--------call evenDigits(0)
--------return 0  // evenDigits(0)
-------return 8 // evenDigits(8)
------return 8 // evenDigits(83)
-----return 84 // evenDigits(834)
----return 842 // evenDigits(8342)
---return 842 // evenDigits(83421)
--return 842 // evenDigits(834211)
-return 8426 // evenDigits(8342116)



The rules appear to be that if you have a valid digit, you have to shift your number and add it. If not, just continue to pass back the current result. How would you do that?

Hope this helps.



This is exactly what I am trying to do, but still have no clue on how to go about it. I have only managed to make it 'print' the output. I can't get it to return a correct integer.
Was This Post Helpful? 0
  • +
  • -

#20 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 28 February 2013 - 06:45 PM

I still can't seem to figure this out. :hang: :hang:
Was This Post Helpful? 0
  • +
  • -

#21 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help me tweak this method

Posted 28 February 2013 - 08:26 PM

We must admit that is not an easy one and it is a vey cute challenge
With 's post you have a clear explanation of what you have to do
Now if you can't code it... may be it is a too advanced topic for your Java knowledge
See you next quarter :)

We must admit that is not an easy one and it is a vey cute challenge
With 's post you have a clear explanation of what you have to do
Now if you can't code it... may be it is a too advanced topic for your Java knowledge
See you next quarter :)
Was This Post Helpful? 0
  • +
  • -

#22 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Help me tweak this method

Posted 01 March 2013 - 04:54 PM

Here's the Tweak


import java.util.Scanner;


public class Odd {

	public Odd() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
System.out.println("Enter Digital Number");
int ent=new Scanner(System.in).nextInt();
int nu=remove(ent);
System.out.println("ODDs out it is "+nu);

	}

	private static int remove(int ent) {
		int j;int[]dig=new int[30];String s="";
	for(int i=0;i<20;i++){
		dig[i]=ent%10;
		ent=(ent-dig[i])/10;
		
		
	}
	for(int i=20;i>=0;i--){
		if(dig[i]%2==0)
		s+=String.valueOf(dig[i]);
		
	}
return Integer.parseInt(s);
	}}

Was This Post Helpful? -1
  • +
  • -

#23 cglidden  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 26-February 13

Re: Help me tweak this method

Posted 01 March 2013 - 05:36 PM

View Postpbl, on 28 February 2013 - 08:26 PM, said:

We must admit that is not an easy one and it is a vey cute challenge
With 's post you have a clear explanation of what you have to do
Now if you can't code it... may be it is a too advanced topic for your Java knowledge
See you next quarter :)/>


"Too advanced for your Java knowledge?" How would you ever expect anyone to learn something if you considered it "too advanced." Didn't know these forums were meant for discouraging someone who is in the process of learning.

Anyways, I managed to figure it out, even though it was "too advanced" for me.. thanks for the help baavgai! Much appreciated.

And for anyone interested in the answer...

public static int evenDigits(int n) {
if(n > -10 && n < 10 ){
	if (n%2==0){return n;} 
            else {return 0;}
} else {
   if ((n%10)%2==0){return (evenDigits(n/10)*10+n%10);} 
else {
		return evenDigits(n/10);
	}
}
}


Messed up the formatting when trying to post it but you get the point :)
Was This Post Helpful? 0
  • +
  • -

#24 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5901
  • View blog
  • Posts: 12,805
  • Joined: 16-October 07

Re: Help me tweak this method

Posted 01 March 2013 - 06:10 PM

Excellent! Glad you got it.

It's only fair, I'll complete the example I gave:
int evenDigits(int n) {
	if(n == 0) { return 0; }
	int digit = n % 10;
	return ((digit % 2) == 0) ? (evenDigits(n/10)*10 + digit) : evenDigits(n/10);
}



You see the dilemma; it's hard to give any more hints without just giving it away.
Was This Post Helpful? 0
  • +
  • -

#25 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help me tweak this method

Posted 03 March 2013 - 06:01 PM

View Postburakaltr, on 27 February 2013 - 08:15 AM, said:

import java.util.Scanner;


public class OddOut {
static Scanner sc=new Scanner(System.in);
static String num="";
	public OddOut() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		char c;
		StringBuilder build = new StringBuilder("");
		// TODO Auto-generated method stub
System.out.println("Enter the Number ");
num=sc.next();

for(int i=0;i<num.length();i++){
	 c=num.charAt(i);
int j=Integer.parseInt(String.valueOf(c));
;
if(checkEven(j)){
	
	build.append(String.valueOf(j));
}
}
int buint=Integer.parseInt(build.toString());
	 System.out.println("Odds Out :  "+buint);

	}

	private static boolean checkEven(int j) {
		// TODO Auto-generated method stub
		if(j%2==0)
		return true;
		return false;
	}
}



Burak, you definitevly have to learn how two correctly indent code
This code is a piece of crap without correct indentation
Was This Post Helpful? 0
  • +
  • -

#26 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Help me tweak this method

Posted 03 March 2013 - 06:07 PM

Yes Right Mr. PBL,

Though I am no expert definitely, but I come from a BackGround Language, FORTRAN.

I am trying to learn Java by Myself Thanks to you and Reputation Greats at DIC.

( Have over 20 Java Books, for aspiration )

And so along the Path, lie the road bumps : Indentation, Naming Conventions...etc.

So Please Bear with my coding, it won't last long that Sooner or Later ( BOB ) I'll adapt to the Almighty JAVA Language Conventions :)
Was This Post Helpful? 1
  • +
  • -

#27 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: Help me tweak this method

Posted 03 March 2013 - 06:14 PM

Start myself with FORTRAN 40 years ago :^:

Just love Java as you seem to do... so spoil it an correctly indent your code :)
Was This Post Helpful? 0
  • +
  • -

#28 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Help me tweak this method

Posted 03 March 2013 - 06:22 PM

Back in 1996 ( my Sophomore Year - at BOUN ) I was taught FORTRAN by the late MR. Prof. Vural Altın.

I fell in Love with the Language, and Even, yes please do not laugh, I tried to write code that could "tweak " the String manipulative Restrictions of the Language ( And No-Hope for any GUI )

It is Now that I Feel I am docked to the Right Port.

Portal of JAVA .
Was This Post Helpful? 0
  • +
  • -

#29 burakaltr  Icon User is online

  • D.I.C Regular

Reputation: 91
  • View blog
  • Posts: 274
  • Joined: 07-November 10

Re: Help me tweak this method

Posted 03 March 2013 - 06:37 PM

Also I am a Fan ( Not a Great One ) of your country's Neil Young.

Love His " Rockin' in the Free World "

Just wanted to mention.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2