Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-14-2007, 04:06 AM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
Rep Power: 0
hey is on a distinguished road
Default VeryLong Integer.. help
Hi guys,

please, help me)))

I am trying to create a VEreyLong Integer class.. I am using ArrayList..

but I have some questions..
this is my code (just the beginning)

Code:
package VeryLong;
import java.util.*;
public class VeryLong{
	public VeryLong(ArrayList<Integer> a){
		ArrayList<Integer> verylong = new ArrayList<Integer>();
		System.out.println("number is created");
		
	};
	public ArrayList<Integer> Readln(){
		
		int i=0, tempint=0;
		ArrayList<Integer> verylong = new ArrayList<Integer>();
		
		
		Scanner in = new Scanner(System.in);
		String str;
		str=in.next();
		while (Character.isDigit(str.charAt(i)))
		   {
		   	tempint = Integer.parseInt( Character.toString( str.charAt(i) ) );
			verylong.add(tempint);
			i++; 	
		   };	
		
			
		return verylong;
	}
}
so, in readln method, I am trying to read a string from console, then write each char (which is digit) to ArrayList, then I have to claim that that created ArrayList is my class VeryLong integer, right? But in this case, ArrayList functions will not be available for me.. So do I need to make my class VeryLong smth like
VeryLong extends ArrayList???? But in this case, how do I block other ArrayList methods, which I don't need..
I know, that it sounds really confusing.. but please, help me!!!)))
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 12-14-2007, 04:16 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
I think what you want to do is make the ArrayList a field in your class.

Code:
package VeryLong;
import java.util.*;
public class VeryLong{

        private ArrayList<Integer> longInt;
	public VeryLong(ArrayList<Integer> a){
		this.longInt = a;
		System.out.println("number is created");
		
	};
	public ArrayList<Integer> Readln(){
		
		int i=0, tempint=0;
		Scanner in = new Scanner(System.in);
		String str;
		str=in.next();
		while (Character.isDigit(str.charAt(i)))
		   {
		   	tempint = Integer.parseInt( Character.toString( str.charAt(i) ) );
			this.longInt.add(tempint);
			i++; 	
		   };	
		
			
		return this.longInt;
	}
}
this way the array just lives in the class. It is private so other classes cannot call ArrayList methods on it directly. If you need use of any of the ArrayLists just set up a public method in your class that calls them.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 12-14-2007, 04:30 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
Rep Power: 0
hey is on a distinguished road
Default
em.. So, in that way, longInt will be my very long integer itself.. right? Thank you so much.! I still don't understand it completely, but I think I am on the right way.)))
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 12-14-2007, 10:28 PM
hey hey is offline
Member
 
Join Date: Dec 2007
Posts: 21
Rep Power: 0
hey is on a distinguished road
Default
Originally Posted by ShoeNinja View Post
I think what you want to do is make the ArrayList a field in your class.

Code:
package VeryLong;
import java.util.*;
public class VeryLong{

        private ArrayList<Integer> longInt;
	public VeryLong(ArrayList<Integer> a){
		this.longInt = a;
		System.out.println("number is created");
		
	};
	public ArrayList<Integer> Readln(){
		
		int i=0, tempint=0;
		Scanner in = new Scanner(System.in);
		String str;
		str=in.next();
		while (Character.isDigit(str.charAt(i)))
		   {
		   	tempint = Integer.parseInt( Character.toString( str.charAt(i) ) );
			this.longInt.add(tempint);
			i++; 	
		   };	
		
			
		return this.longInt;
	}
}
this way the array just lives in the class. It is private so other classes cannot call ArrayList methods on it directly. If you need use of any of the ArrayLists just set up a public method in your class that calls them.
I am sorry for the stupid questions, but how can I test it?
This is what I have:
Code:
import VeryLong.VeryLong;
import java.util.*;

class main{
	public static void main(String[] args){
		VeryLong.Readln();

	}

}
but I cannot do this, because Readln is non-static, but I also cannot call constructor,because I need to read inforamtion from the console first.. any ideas?

Thanks a lot!!!
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 12-14-2007, 10:48 PM
ShoeNinja's Avatar
Senior Member
 
Join Date: Oct 2007
Posts: 124
Rep Power: 0
ShoeNinja is on a distinguished road
Send a message via AIM to ShoeNinja
Default
You need to create a VeryLong object.

Code:
VeryLong vl = new VeryLong(**Some array list here**);
You'll probably want to add another constructor to the very long class that looks like this.

Code:
pucbli VeryLong(){
      this.longInt = new ArrayList<Integer>();
}
This will create an empty VeryLong instead of copying a passed ArrayList. To use this constructor:

Code:
VeryLong vl = new VeryLong();
After the object is created, you can call it methods.

Code:
vl.Readln()  //by the way, it is a good idea to start your method names with lower case letters
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Breaking down an integer Emily New To Java 1 03-06-2008 07:39 PM
Create a VeryLong class that will store an integer of arbitrary length. hey New To Java 2 12-12-2007 06:01 PM
Short/Integer mew New To Java 3 12-06-2007 10:28 PM
Integer vs int bugger New To Java 1 11-14-2007 10:13 PM
Help with Integer in java susan New To Java 1 07-14-2007 06:25 AM


All times are GMT +2. The time now is 12:19 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org