calling classes and methods to enter 5 digit number

  • (2 Pages)
  • +
  • 1
  • 2

20 Replies - 1051 Views - Last Post: 02 October 2012 - 07:07 AM Rate Topic: -----

#1 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 10:55 AM


public class Number {

	
	public static void main(String[] args) {
		
		
	int numberEntered;
	int firstDigit;
	int secondDigit;	
	int thirdDigit;
	int fourthDigit;	
	int fifthDigit;	
		
		getNumberFromUser getNumberFromUserObject = new getNumberFromUser();
		getNumberFromUserObject.input();
		
		calculateDigits calculateDigitsObject = new calculateDigits();
		calculateDigitsObject.digits();
		
		
		displayDigits displayDigitsObject = new displayDigits();
		displayDigitsObject.display();
	}

}

import java.util.Scanner;


public class getNumberFromUser {

	public void input ()  {
		
		Scanner input = new Scanner (System.in);
		System.out.print("Enter a five digit number:   ");
		int numberEntered = input.nextInt();
	
	
	}
	
}




public class calculateDigits {
	
	
	public void digits() {
		
		
		int numberEntered = 0;
		int firstDigit = (numberEntered/10000);
		int secondDigit = (numberEntered/1000%10);
		int thirdDigit = (numberEntered/100%10);
		int fourthDigit = (numberEntered/10%10);
		int fifthDigit = (numberEntered/1%10);
		
		
		
	}
	
	
	
	
	
	

}

public class displayDigits {
	
	public void display ()  {
		
		int firstDigit = 0;
		int secondDigit = 0;
		int thirdDigit = 0;
		int fourthDigit = 0;
		int fifthDigit = 0;
		
		System.out.println(firstDigit);
		System.out.println(secondDigit);
		System.out.println(thirdDigit);
		System.out.println(fourthDigit);
		System.out.println(fifthDigit);
		
	}
	
	
	
	
	

}




The first two classes of this java project are working fine.

I need to create a program that will allow me to enter a five digit number and split them into 5 seperate digits.

The classes calculateDigits and displayDigits are the ones that I need help with.

I have done the math already in a previous program , I just don't know how to implement it into a java project with multiple classes.

Is This A Good Question/Topic? 0
  • +

Replies To: calling classes and methods to enter 5 digit number

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,736
  • Joined: 19-March 11

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 11:20 AM

Okay, I know this is not your question, but could you not use an array and loop over it?
Every time you create five separate named objects and have to do the same thing five different times to manipulate them, God kills five separate named Kitten objects. The Garbage Collector is coming for them - can you hear them crying? Have pity!

Also, please follow standard naming conventions. Classes are named with InitialCapitals, objects and variables are named using CamelCase.


Okay, that's out of the way.


To create an object that does calculations for you, just give that object a method that takes the input, and does the calculation returning a result, or else performs a service and returns no result.

public int[] splitStringIntoDigits(String input)
{
  int[] returnArray = new int[5];
  // do calculation here
  return returnArray;  // return the result. 
}


public void displayDigits(int[] digits)
{
  // display them
  // return nothing because there's nothing to return
}




Since these objects are not concerned with their own data, they're just performing a service, the methods can be static if you like. This means that the method cannot refer to any instance data of the object that holds it, which is fine if all it's doing is calculating and returning values.
Was This Post Helpful? 1
  • +
  • -

#3 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 11:39 AM

I don't want to use arrays in my class the only thing I need help with is calling varibales so that all classes can read them. I only wanted to make 5 variables, numberEntered, firstDigit, secondDigit, thirdDIgit, fourthDigit and fifthDigit in the main class Number but I don'r want to have to re initialize the variable in the other classes.
Was This Post Helpful? 0
  • +
  • -

#4 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7577
  • View blog
  • Posts: 12,736
  • Joined: 19-March 11

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 12:01 PM

View PostCoderman23, on 01 October 2012 - 01:39 PM, said:

I don't want to use arrays in my class


And you don't care about the kittens? Okay, that's fine.


Quote

the only thing I need help with is calling varibales so that all classes can read them. I only wanted to make 5 variables, numberEntered, firstDigit, secondDigit, thirdDIgit, fourthDigit and fifthDigit in the main class Number but I don'r want to have to re initialize the variable in the other classes.


You could do this - give them all public access. However, if you do this, you're doing it wrong.
Was This Post Helpful? 0
  • +
  • -

#5 ryanisis  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 12
  • Joined: 24-September 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 12:06 PM

I think you're going about this in entirely the wrong way. This program could be much more simplistic.

Do you absolutely have to have these methods in separate classes? It seems really unnecessary to me.
Most of this could be crammed in to one relatively simple method or maybe two if you really want to.

I will suggest that what you should do is read the input number as a String instead of an int.
That way you can split the number using something along the lines of :
import java.util.Scanner;

public class IntSplitter {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		System.out.print("Enter a 5 digit Number : ");
		String userInput = sc.nextLine();
		
		char firstChar = userInput.charAt(0);
		char secondChar = userInput.charAt(1);
		char thirdChar = userInput.charAt(2);
		char fourthChar = userInput.charAt(3);
		char fifthChar = userInput.charAt(4);
		int firstDigit = Character.getNumericValue(firstChar);
		int secondDigit = Character.getNumericValue(secondChar);
		int thirdDigit = Character.getNumericValue(thirdChar);
		int fourthDigit = Character.getNumericValue(fourthChar);
		int fifthDigit = Character.getNumericValue(fifthChar);
		
		System.out.println("\n" + firstDigit);
		System.out.println(secondDigit);
		System.out.println(thirdDigit);
		System.out.println(fourthDigit);
		System.out.println(fifthDigit);
	}
}



However, if you ABSOLUTELY have to make the program as complicated as you're trying to, you can just split the same
method up in to different classes. Such as :
package misc;

import java.util.Scanner;

public class IntSplitter {
	
	public static class getUserInput {
		public static String input(Scanner sc) {
			System.out.print("Enter a 5 digit Number : ");
			String userInput = sc.nextLine();
			
			return userInput;
		}
	}

	public static class displayDigits {
		public static void display(int firstDigit, int secondDigit, int thirdDigit,
				int fourthDigit, int fifthDigit) {
			System.out.println("\n" + firstDigit);
			System.out.println(secondDigit);
			System.out.println(thirdDigit);
			System.out.println(fourthDigit);
			System.out.println(fifthDigit);
		}
	}
	
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		getUserInput newInput = new getUserInput();
		String userInput = newInput.input(sc);
		
		char firstChar = userInput.charAt(0);
		char secondChar = userInput.charAt(1);
		char thirdChar = userInput.charAt(2);
		char fourthChar = userInput.charAt(3);
		char fifthChar = userInput.charAt(4);
		int firstDigit = Character.getNumericValue(firstChar);
		int secondDigit = Character.getNumericValue(secondChar);
		int thirdDigit = Character.getNumericValue(thirdChar);
		int fourthDigit = Character.getNumericValue(fourthChar);
		int fifthDigit = Character.getNumericValue(fifthChar);
		
		displayDigits.display(firstDigit, secondDigit, thirdDigit, fourthDigit,
				fifthDigit);
	}	
}


Was This Post Helpful? 1
  • +
  • -

#6 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 02:35 PM






import java.util.Scanner;


public class Assign1 {

	
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner (System.in);
		
		
		//Variable Declaration
	
		System.out.print("Enter a five digit number:   ");
		int number = input.nextInt();
		
		while (number < 10000 || number > 99999)  {
			
		System.out.println("That is NOT a five digit number!  ");
		System.out.print("Please re-enter a five digit number:  ");
		number = input.nextInt();

		}

		if (number > 9999 && number < 99999) {
			
			System.out.println("The number is  ");
			
			System.out.println(number/10000);
			System.out.println(number/1000%10);
			System.out.println(number/100%10);
			System.out.println(number/10%10);
			System.out.println(number/1%10);
			
			
		}
		
		
		
		if (number < 9999) {
			
			System.out.println("That is NOT a five digit number.  ");
			
		}
		

		if (number > 99999) {
			
			System.out.println("That is NOT a five digit number.  ");
			
		}
	

		
		
		
		
	
	}

}




Was This Post Helpful? 0
  • +
  • -

#7 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 02:39 PM

What's your question?
Was This Post Helpful? 0
  • +
  • -

#8 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 02:40 PM

I want to recreate this class using multiple classes.

This is what I am supposed to write but I have Number and getNumberFromUser down.
I am just having trouble getting the calculateDigits class and displayDigits to work.

Your class should be called Number. It should contain 6 fields of type int
named correspondingly for numberEntered, firstDigit, secondDigit, thirdDigit,
fourthDigit and fifthDigit. You should have the following methods:
a) default constructor initializes to "no data" ie zeros.
B) getNumberFromUser prompts user for the five-digit number and reads in
their input
c) calculateDigits calculates each of the five digits
d) displayDigits displays each of the digits on a separate line
Rewrite your main method to declare a reference variable to an object of this
class, use new to create (instantiate) the object, and then call the methods
on the object.
.
Was This Post Helpful? 0
  • +
  • -

#9 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1377
  • View blog
  • Posts: 3,035
  • Joined: 05-April 11

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 02:40 PM

Why do you want to recreate it with multiple classes?

You are making sure that you get an input in the range 10001 - 99999, so why do you keep checking the range of the input all through your code?
Was This Post Helpful? 0
  • +
  • -

#10 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 02:43 PM

The instructions are pretty explicit. Can't you follow them? Try, setting up the classes as instructed, and come back with your code if/when you need help.
Was This Post Helpful? 0
  • +
  • -

#11 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 03:06 PM

View PostGregBrannon, on 01 October 2012 - 02:39 PM, said:

What's your question?



My question is how would I implement this code into multiple classes?
Was This Post Helpful? 0
  • +
  • -

#12 Kakerergodt  Icon User is offline

  • D.I.C Head

Reputation: 87
  • View blog
  • Posts: 201
  • Joined: 01-May 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 04:53 PM

View PostCoderman23, on 01 October 2012 - 02:40 PM, said:

Your class should be called Number. It should contain 6 fields of type int
named correspondingly for numberEntered, firstDigit, secondDigit, thirdDigit,
fourthDigit and fifthDigit. You should have the following methods:
a) default constructor initializes to "no data" ie zeros.
B) getNumberFromUser prompts user for the five-digit number and reads in
their input
c) calculateDigits calculates each of the five digits
d) displayDigits displays each of the digits on a separate line
Rewrite your main method to declare a reference variable to an object of this
class, use new to create (instantiate) the object, and then call the methods
on the object.


As GregBrannon said the instructions are pretty clear, you're not supposed to have plenty of classes, but 1 main class to "run" the program and a second class Numbers with the methods outlined in a through d:
Main class:
public static main(String[] arg)
   //Rewrite your main method to declare a reference variable to an object of this
   //class, use new to create (instantiate) the object, and then call the methods
   //on the object.


Numbers class:
datafield for each number

public Numbers()
   // a) default constructor  initializes to "no data" ie zeros.

public void getNumberFromUser()
   //B)/> prompts user for the five-digit number and reads in their input

public void calculateDigits()
   //c) calculates each of the five digits

public void displayDigits()
   //d) displays each of the digits on a separate line


Was This Post Helpful? 0
  • +
  • -

#13 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 06:57 PM


import java.util.Scanner;


public class Number {
	
	int numberEntered = 0;
	int firstDigit = 0;
	int secondDigit = 0;
	int thirdDigit = 0;
	int fourthDigit = 0;
	int fifthDigit = 0;
	
	




	
	public void getNumberFromUser()  {
		
		Scanner input = new Scanner (System.in);
		
		System.out.print("Enter a five digit number:   ");
		numberEntered = input.nextInt();

		
		
	}
	
	
	public void calculateDigits() {
		
if (numberEntered > 9999 || numberEntered < 99999)  {
			
			firstDigit  = (numberEntered/10000);
			secondDigit = (numberEntered/1000%10);
			thirdDigit  = (numberEntered/100%10);
			fourthDigit = (numberEntered/10%10);
			fifthDigit  = (numberEntered/1%10);
			
			
			}
		
	}
	
	
	public void displayDigits()  {
		
		if (numberEntered > 9999 || numberEntered < 99999)  {
			
		System.out.println("The number is:  ");
		
		System.out.println(firstDigit);
		System.out.println(secondDigit);
		System.out.println(thirdDigit);
		System.out.println(fourthDigit);
		System.out.println(fifthDigit);
		
		
		}
		
		
		
		
	}
	
	
}
	
	


whe I run the program, everything works except the output of the numbers (i.e they all display as 0)
Was This Post Helpful? 0
  • +
  • -

#14 ihatesegfault  Icon User is offline

  • New D.I.C Head

Reputation: 17
  • View blog
  • Posts: 47
  • Joined: 24-September 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 07:10 PM

First off: what number are you trying and what is the output. "numbers don't compute" is ambiguious and doesn't help us help you.

I assume you want the number entered to be a 5 digit number. In that case you need to change your if conditions (lines 33, 49). in psuedo: if the number > 9999 digits OR the number < 99999 then ...

try sticking 10 in: if false OR true then TRUE! the OR needs to be an AND.
also, try 99999 (which is a 5 digit number). with your current code it does go through, but once you switch the OR and AND it doesn't. that's because < is non-inclusive. so use <=

Also modulo is on the same order as multiplication and division in Java order of operations, so add some ().
Was This Post Helpful? 1
  • +
  • -

#15 Coderman23  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 01-October 12

Re: calling classes and methods to enter 5 digit number

Posted 01 October 2012 - 07:33 PM

I changed the modulus (I added brackets)

But the psuedocode is correct because I made a program that I exactly want to model but it only is a single class and only has a main method, it works perfectly. But for some reason the class I made with the three methods doesnt seem to work. Where I call the methods into the main class.



// Here is the program that I am trying to model but not using the main mehtod using a seperate class and multiple methods and calling those methods in the main method





import java.util.Scanner;


public class Assign1 {

	
	
	public static void main(String[] args) {
		
		Scanner input = new Scanner (System.in);
		
		
		//Input
	
		System.out.print("Enter a five digit number:   ");
		int number = input.nextInt();
		
		
		// If the number is not five digits
		
		while (number < 10000 || number > 99999)  {
			
		System.out.println("That is NOT a five digit number!  ");
		System.out.print("Please re-enter a five digit number:  ");
		number = input.nextInt();

		}

		// If the number is five digits
		
		if (number > 9999 && number < 99999) {
			
			System.out.println("The number is  ");
			
			System.out.println(number/10000);
			System.out.println(number/1000%10);
			System.out.println(number/100%10);
			System.out.println(number/10%10);
			System.out.println(number/1%10);
			
			
		}
		
	
	
	}

}




Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2