Question

In: Computer Science

Write a program in java to determine whether a given function F from the set {0,1,2,3,...,m}...

Write a program in java to determine whether a given function F from the set {0,1,2,3,...,m} to (0,1,2,3,...,n} is one-to-one and/or onto. The inputs to your program will be:

- an integer m

- an integer n, and

- a two dimensional boolean array F of size (m+1) x (n+1), where F[ i , j ] = 1 if F(i) = j.

Solutions

Expert Solution

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class Main
{
public static boolean checkOneToOne(int m, int n, int F[][]) {
// check if every column has less than or equal to one 1.
for (int j = 0; j <= n; j++) {
int count_one = 0;
for (int i = 0; i <= m; i++) {
count_one += F[i][j];
}
if (count_one > 1)
return false;
}
return true;
}
  
public static boolean checkOnto(int m, int n, int F[][]) {
// check if every column has atleast one 1.
for (int j = 0; j <= n; j++) {
int count_one = 0;
for (int i = 0; i <= m; i++) {
count_one += F[i][j];
}
if (count_one == 0)
return false;
}
return true;
}
  
   public static void main(String[] args) throws IOException {
       BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       System.out.print("Enter m: ");
       int m = Integer.parseInt(br.readLine());
       System.out.print("Enter n: ");
       int n = Integer.parseInt(br.readLine());
       int F[][] = new int[m + 1][n + 1];
       for (int i = 0; i <= m; i++) {
       for (int j = 0; j <= n; j++) {
       System.out.print("Enter F[" + i + ", " + j +"]: ");
       F[i][j] = Integer.parseInt(br.readLine());
       }
       }
       if (checkOneToOne(m, n, F))
       System.out.println("Function is one-to-one.");
       else
       System.out.println("Function is not one-to-one.");
       if (checkOnto(m, n, F))
       System.out.println("Function is onto.");
       else
       System.out.println("Function is not onto.");
       br.close();
   }
}


Related Solutions

• This lab, you will write a Java program to determine if a given Sudoku puzzle...
• This lab, you will write a Java program to determine if a given Sudoku puzzle is valid or not. • You determine if the puzzle is complete and valid, incomplete, or is invalid. • A puzzle is a 2-dimensional array 9x9 array. Each element contains the numbers 1 – 9. A space may also contain a 0 (zero), which means the spot is blank. • If you don’t know how a Sudoku Puzzle works, do some research, or download...
1.            Determine whether the function f from { a, b, c, d } to {a,...
1.            Determine whether the function f from { a, b, c, d } to {a, b, c, d, e} is injective (one-to-one), surjective (onto) and/or bijective (one-to- one correspondence) : f(a) = a,            f(b) = c,            f(c) = b, f(d) = e a. Is this function injective?              . surjective?              . bijective?              . If your answer is no for any of the above, explain:             b. Is there an inverse for this function?              . c. Is the composition f...
For the following exercises, determine whether or not the given function f is continuous everywhere...f(x) = log2 (x)
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = log2 (x)
For the following exercises, determine whether or not the given function f is continuous everywhere...f(x) = tan(x) + 2
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = tan(x) + 2
For the following exercises, determine whether or not the given function f is continuous everywhere....f(x) = 2x + 5/x
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = 2x + 5/x
For the following exercises, determine whether or not the given function f is continuous everywhere....f(x) = sec(x) − 3.
For the following exercises, determine whether or not the given function f is continuous everywhere. If it is continuous everywhere it is defined, state for what range it is continuous. If it is discontinuous, state where it is discontinuous.f(x) = sec(x) − 3.
you are to write a program in Java, that reads in a set of descriptions of...
you are to write a program in Java, that reads in a set of descriptions of various geometric shapes, calculates the areas and circumferences of the shapes, and then prints out the list of shapes and their areas in sorted order from smallest to largest area. There are four possible shapes: Circle, Square, Rectangle, and Triangle. The last is always an equilateral triangle. The program should read from standard input and write to standard output. The program should read until...
determine whether the given function is even, odd, or neither. Please write a code in MatLab...
determine whether the given function is even, odd, or neither. Please write a code in MatLab to solve this problem below: 1.f(x) = sin 3x please only use Matlab to solve this problem
Write a Java program to read in words from the given file “word.txt”. a. Prompt the...
Write a Java program to read in words from the given file “word.txt”. a. Prompt the user for two words b. Print out how many words in the file fall between those words c. If one of the two words is not contained in the file, print out which word is not found in the file d. If both words are not found in the file, print out a message e. Sample output: Please type in two words: hello computer...
Write a program in JAVA to create the move set of a Pokémon, and save that...
Write a program in JAVA to create the move set of a Pokémon, and save that move set to a file. This program should do the following: Ask for the pokemon’s name. Ask for the name, min damage, and max damage of 4 different moves. Write the move set data into a file with the pokemon’s name as the filename. The format of the output file is up to you, but keep it as simple as possible
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT