Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- class Ex2
- {
- public static void main(String[] args)
- {
- Scanner in = new Scanner(System.in);
- LispList<Integer> ls = LispList.empty();
- System.out.print("Enter a list of integers (in the format [$,$,$]): ");
- String user = in.nextLine();
- ls = parseIntLispList(user);
- int len = length(ls);
- System.out.print("The list you entered is: ");
- System.out.println(user);
- System.out.println("There are " +len+ " integers in the list" );
- ////////////////////////////////
- System.out.println(" ");
- System.out.println("Enter an integer:");
- int n = in.nextInt();
- boolean Answer = member(ls, n);
- if (Answer == true){
- System.out.println("Yes " + n + " is in the list");
- }
- else{
- System.out.println("No " +n+ " is not in the list");
- }
- //////////////////////////
- System.out.println(" ");
- System.out.println("Enter an integer:");
- int o = in.nextInt();
- int number = count(ls, o);
- System.out.println(o + " occurs in the list " + number + " times.");
- }
- public static int length(LispList<Integer> ls)
- {
- int count = 0;
- for(LispList<Integer> ls2=ls; !ls2.isEmpty(); ls2=ls2.tail())
- {
- int n = ls2.head();
- count = count+1;
- }
- return count;
- }
- public static boolean member (LispList<Integer> ls, int n){
- for (LispList<Integer> ls3=ls; !ls3.isEmpty(); ls3=ls3.tail())
- {
- int M = ls3.head();
- if (M==n || ls3.tail().equals(n)){
- return true;
- }
- }
- return false;
- }
- public static int count (LispList<Integer> ls, int o){
- int check = 0;
- for(LispList<Integer> ls4=ls; !ls4.isEmpty(); ls4=ls4.tail())
- {
- int P = ls4.head();
- if (P==o || ls4.tail().equals(o)){
- check = check + 1;
- }
- }
- return check;
- }
- public static LispList<Integer> parseIntLispList(String user) //This is the helper method taken from UseLispLists2
- {
- String line = user.trim();
- String contents = line.substring(1,line.length()-1).trim();
- if(contents.length()==0)
- return LispList.empty();
- String[] nums = contents.split(",");
- LispList<Integer> list = LispList.empty();
- for(int i=nums.length-1; i>=0; i--)
- {
- String num = nums[i].trim();
- list = list.cons(Integer.parseInt(num));
- }
- return list;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement