Question
This is programming assignment of discrete structure. Anyone know how to do it. Pls help! Thanks.
Problem 1[50 pts]: Your program must take as input a filename. This input file has the following format. The first line contains five positive integers separated by white space characters. Call them a, b,c, m, n. The next m lines describe the pairs in a relation Ri on the set {1, 2, , a x(1, 2, b), and the n lines following it describe the pairs in a relation R2 on the set (1,2,... ,b) x (1,2,...e. Each pair in the relation will be specified as (r, y). An example input file is the following: 2 4521 (4, 5) Your code must output the following, each on a new line. The last pair outputs a set of pairs - each of them must be on a new line as described below. YES if the relation Ri is a function and NO otherwise. . YES if the relation R2 is a function and NO otherwise. ·YES if relation Ri is a function and it is onto, NA if it is not a function, NO if it is a function and not onto. YES if the relation R2 is a function and it is injective, NA if it is not a function, NO if it is a function and not injective. . If both are functions then compute the function go f and output it as pairs where the first coordinate in the pair are in sorted order. For example, if g o f maps 1 to 10, 2 to 4 and 3 to 15 your code must output for this part, the following three lines. (2.4) (3,15) If any one of them is not a function, then just output NA for this part.
media%2Ff4b%2Ff4b60a30-2786-4d90-8a17-88
0 0
Add a comment Improve this question Transcribed image text
Answer #1

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Scanner;


public class functionFinder {

   /**
   * @param args
   */
   public static void main(String[] args) {
       String fileName;
       System.out.println("Enter File Name");
       Scanner s=new Scanner(System.in);
       fileName=s.next();
       try {
           s=new Scanner(new File(fileName));
       } catch (FileNotFoundException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
       int a=s.nextInt();
       int b=s.nextInt();
       int c=s.nextInt();
       int m=s.nextInt();
       int n=s.nextInt();
       ArrayList<MyPair> arr1=new ArrayList<>();
       ArrayList <MyPair> arr2=new ArrayList<>();
       ArrayList <MyPair> arr3= new ArrayList<>();
       for(int i=0;i<m;i++)
       {
           int d=s.nextInt();
           int e=s.nextInt();
           if(d<=a && e<=b)
           {  
               MyPair p=new MyPair(d, e);
               arr1.add(p);
           }
       }
       System.out.println("Reached Here");
       for(int i=0;i<n;i++)
       {
           int d=s.nextInt();
           int e=s.nextInt();
           if(d<=b && e<=c)
           {  
               MyPair p=new MyPair(d,e);
               arr2.add(p);
           }
       }
      
       boolean onetoOne1=checkOnetoOne(arr1,a);
  
       boolean onetoOne2=checkOnetoOne(arr2,b);
       boolean onto1=checkOnto(arr1,b);
       boolean onto2=checkOnto(arr2,c);
       if(onetoOne1)
       {
           System.out.println("YES");
       }
       else
       {
           System.out.println("NO");
       }
       if(onetoOne2)
       {
           System.out.println("YES");
       }
       else
       {
           System.out.println("NO");
       }
       if(onetoOne1 && onto1)
       {
           System.out.println("YES");
       }
       else if(onetoOne1)
       {
           System.out.println("NA");
       }
       else
       {
           System.out.println("NO");
       }
       if(onetoOne2 && onto2)
       {
           System.out.println("YES");
       }
       else if(onetoOne2)
       {
           System.out.println("NA");
       }
       else
       {
           System.out.println("NO");
       }
      
       if(onetoOne1 && onetoOne2 && onto1 && onto2 )
       {
           for(int i=0;i<arr2.size();i++)
           {
               int x,y,temp;
               y=0;
               x=arr2.get(i).key();
               temp=arr2.get(i).value();
               for(int j=0;j<arr1.size();j++)
               {
                   if(temp==arr1.get(j).key())
                   {
                       y=arr1.get(j).value();
                       break;
                   }
               }
               MyPair p=new MyPair(x,y);
               arr3.add(p);
           }
           for(int i=0;i<arr3.size();i++)
           {
               System.out.println("("+arr3.get(i).key()+","+arr3.get(i).value()+")");
           }
       }
      

   }

   public static boolean checkOnetoOne(ArrayList<MyPair> arr,int a)
   {
       boolean isOnetoOne=false;
       boolean[] array=new boolean[a+1];
      
       for(int i=0;i<arr.size();i++)
       {
           if(!array[arr.get(i).key()])
           {
              
               array[arr.get(i).key()]=true;
           }
           else
           {
               return false;
              
           }
          
       }
      
       for(int i=1;i<=a;i++)
       {
           if(!array[i])
           {
               System.out.println(i);
               return false;
           }
       }
       isOnetoOne=true;
       return isOnetoOne;
   }
   public static boolean checkOnto(ArrayList<MyPair> arr,int b)
   {
       boolean isOnto=false;
       boolean[] array=new boolean[b+1];
       for(int i=0;i<arr.size();i++)
       {
           if(!array[arr.get(i).value()])
           {
               array[arr.get(i).value()]=true;
           }
          
       }
       for(int i=1;i<=b;i++)
       {
           if(!array[i])
           {
               return false;
           }
       }
       isOnto=true;
       return isOnto;
      
   }
  

}


//MyPair Class


class MyPair
{
    private final int key;
    private final int value;

    public MyPair(int aKey, int aValue)
    {
        key   = aKey;
        value = aValue;
    }

    public int key()   { return key; }
    public int value() { return value; }
}

Add a comment
Know the answer?
Add Answer to:
This is programming assignment of discrete structure. Anyone know how to do it. Pls help! Thanks....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hi can anyone help me with this question? Please use python when you do it. THANKS...

    Hi can anyone help me with this question? Please use python when you do it. THANKS 1. Arithmetic trees 50 marks You are given an input file with multiple pairs of input lines. The first line of each pair is a tree given as a predecessor array. The second line is the value at the corresponding node. Values at leaf nodes (nodes with no children) are integers. At non-leaf nodes, the two possible values are or * The tree represents...

  • . . In this programming assignment, you need to write a CH+ program that serves as...

    . . In this programming assignment, you need to write a CH+ program that serves as a very basic word processor. The program should read lines from a file, perform some transformations, and generate formatted output on the screen. For this assignment, use the following definitions: A "word" is a sequence of non-whitespace characters. An "empty line" is a line with no characters in it at all. A "blank line" is a line containing only one or more whitespace characters....

  • C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require...

    C++ please Programming Assignment #6 Help Me Find The Secret Message Description: This assignment will require that you read in an encrypted message from a file, decode the message, and then output the message to a file. The encryption method being used on the file is called a shift cipher (Info Here). I will provide you with a sample encrypted message, the offset, and the decrypted message for testing. For this project I will provide you main.cpp, ShiftCipher.h, and ShiftCipher.cpp....

  • I need help with this assignment in C++, please! *** The instructions and programming style detai...

    I need help with this assignment in C++, please! *** The instructions and programming style details are crucial for this assignment! Goal: Your assignment is to write a C+ program to read in a list of phone call records from a file, and output them in a more user-friendly format to the standard output (cout). In so doing, you will practice using the ifstream class, I'O manipulators, and the string class. File format: Here is an example of a file...

  • Java 8 Braces You are designing a compiler for a C++ program and need to check...

    Java 8 Braces You are designing a compiler for a C++ program and need to check that braces in any given file are balanced Braces in a string are considered to be balanced if the following criteria are met: All braces must be closed. Braces come in pairs of the form 0.0andl1. The left brace opens the pair, and the right one closes it In any set of nested braces, the braces between any pair must be closed For example,...

  • Need help with java programming. Here is what I need to do: Write a Java program...

    Need help with java programming. Here is what I need to do: Write a Java program that could help test programs that use text files. Your program will copy an input file to standard output, but whenever it sees a “$integer”, will replace that variable by a corresponding value in a 2ndfile, which will be called the “variable value file”. The requirements for the assignment: 1.     The input and variable value file are both text files that will be specified in...

  • I need help understanding this programming assignment. I do not understand it at all. Someone provided...

    I need help understanding this programming assignment. I do not understand it at all. Someone provided me with the code but when I run the code on eclipse it gives an error. Please explain this assignment to me please. Here is the code: import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class TextEditor { public static List<String> lines = new ArrayList<String>(); public static void main(String[] args) throws IOException { Scanner s = new...

  • For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java...

    For this assignment, you will use your knowledge of arrays and ArrayLists to write a Java program that will input a file of sentences and output a report showing the tokens and shingles (defined below) for each sentence. Templates are provided below for implementing the program as two separate files: a test driver class containing the main() method, and a sentence utilities class that computes the tokens and shingles, and reports their values. The test driver template already implements accepting...

  • 1 Objective The rules for a new type of programming contest provides a list of problems,...

    1 Objective The rules for a new type of programming contest provides a list of problems, their respective score in integer points, and a statistically valid estimate of the time it takes to solve the problem. This duration or time is expressed in integer hours. To help with a solution strategy the contest organizers reveal there is a dynamic programming solution enabling all the contestants to maximize their score - and stay within the time limits for the contest. One...

  • I am really struggling with this assignment, can anyone help? It is supposed to work with...

    I am really struggling with this assignment, can anyone help? It is supposed to work with two files, one that contains this data: 5 Christine Kim # 30.00 3 1 15 Ray Allrich # 10.25 0 0 16 Adrian Bailey # 12.50 0 0 17 Juan Gonzales # 30.00 1 1 18 J. P. Morgan # 8.95 0 0 22 Cindy Burke # 15.00 1 0 and another that contains this data: 5 40.0 15 42.0 16 40.0 17 41.5...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT