Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.41 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class Ex2
  4. {
  5.  public static void main(String[] args)
  6.  {
  7.   Scanner in = new Scanner(System.in);
  8.   LispList<Integer> ls = LispList.empty();
  9.   System.out.print("Enter a list of integers (in the format [$,$,$]): ");
  10.   String user = in.nextLine();
  11.  
  12.   ls = parseIntLispList(user);
  13.   int len = length(ls);
  14.  
  15.   System.out.print("The list you entered is: ");
  16.   System.out.println(user);
  17.   System.out.println("There are " +len+ " integers in the list" );
  18.  
  19.  
  20.   ////////////////////////////////
  21.   System.out.println(" ");
  22.   System.out.println("Enter an integer:");
  23.   int n = in.nextInt();
  24.   boolean Answer = member(ls, n);
  25.   if (Answer == true){
  26.       System.out.println("Yes " + n + " is in the list");
  27.     }
  28.     else{
  29.     System.out.println("No " +n+ " is not in the list");
  30.     }
  31.   //////////////////////////
  32.   System.out.println(" ");
  33.   System.out.println("Enter an integer:");
  34.   int o = in.nextInt();
  35.   int number = count(ls, o);
  36.   System.out.println(o + " occurs in the list " +  number + " times.");  
  37.  }
  38.  
  39.  
  40.  public static int length(LispList<Integer> ls)
  41.  {  
  42.   int count = 0;
  43.   for(LispList<Integer> ls2=ls; !ls2.isEmpty(); ls2=ls2.tail())
  44.       {
  45.          int n = ls2.head();
  46.          count = count+1;
  47.       }
  48.   return count;
  49.  }
  50.  
  51.  
  52.  
  53.  public static boolean member (LispList<Integer> ls, int n){
  54.      for (LispList<Integer> ls3=ls; !ls3.isEmpty(); ls3=ls3.tail())
  55.         {
  56.             int M = ls3.head();
  57.          if (M==n || ls3.tail().equals(n)){
  58.                 return true;
  59.             }
  60.          }
  61.      return false;
  62.  }
  63.    
  64.  
  65.  public static int count (LispList<Integer> ls, int o){
  66.    int check = 0;
  67.   for(LispList<Integer> ls4=ls; !ls4.isEmpty(); ls4=ls4.tail())
  68.       {
  69.          int P = ls4.head();
  70.          if (P==o || ls4.tail().equals(o)){
  71.             check = check + 1;  
  72.             }
  73.       }
  74.   return check;
  75.     }
  76.    
  77. public static LispList<Integer> parseIntLispList(String user) //This is the helper method taken from UseLispLists2
  78.  {
  79.   String line = user.trim();
  80.   String contents = line.substring(1,line.length()-1).trim();
  81.   if(contents.length()==0)
  82.      return LispList.empty();
  83.   String[] nums = contents.split(",");
  84.   LispList<Integer> list = LispList.empty();
  85.   for(int i=nums.length-1; i>=0; i--)
  86.       {
  87.        String num = nums[i].trim();
  88.        list = list.cons(Integer.parseInt(num));
  89.       }
  90.   return list;
  91.  }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement