Question

In: Computer Science

Write a Java program that defines two boolean variables a and b and shows that regardless...

Write a Java program that defines two boolean variables a and b and shows that regardless of the values of a and b, the expressions (a && (a || b)) and a are always the same.

Solutions

Expert Solution

public class logic
{
   public static void main(String args[])
   {
       boolean a = true;
       boolean b = true;
       System.out.println("Value of a now: "+a);
       System.out.println("Value of b now: "+b);
       System.out.println((a &&(a||b)));
       a = true;
       b = false;
       System.out.println("Value of a now: "+a);
       System.out.println("Value of b now: "+b);
       System.out.println((a &&(a||b)));
       a = false;
       b = true;
       System.out.println("Value of a now: "+a);
       System.out.println("Value of b now: "+b);
       System.out.println((a &&(a||b)));
       a = false;
       b = false;
       System.out.println("Value of a now: "+a);
       System.out.println("Value of b now: "+b);
       System.out.println((a &&(a||b)));
   }
}

//The above is the screen shot to refer to the indentation.

//The above is the output of the above code.

// you can see clearly from the output that value of the expression always equal to that of the value of a.

// Here we have tried all possible combination with 2 variable i.e 4 combinations as you can see.

// Please rate the solution and ask your doubts if any in the comments section below.


Related Solutions

Write a program that inputs the values of three Boolean variables, a, b, and c, from...
Write a program that inputs the values of three Boolean variables, a, b, and c, from a “cin” operator (user gives the values be sure to prompt user for what they have to give!). Then the program determines the value of the conditions that follow as true or false. 1. !(a&&b&&c) && ! (a||b||c) 2. !(a||b)&&c Output should include the values of a,b,c ie 0 or 1 in the patterns that follow reflecting the Boolean logic being tested. Where “True”...
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n
Write a Java program to 1. read in the size of a square boolean matrix A...
Write a Java program to 1. read in the size of a square boolean matrix A 2. read in the 0-1 matrix elements 3. read in a positive integer n 4. display A^n Multiplying that matrix to the nth power. Like A^2 = matrix A * matrix A. Elements ONLY can be 0 or 1.
Write a program in Java that will take as input two sets A and B, and...
Write a program in Java that will take as input two sets A and B, and returns a list of all functions from A to B.
(JAVA) Create a program that prompts the user for an age input. The program defines the...
(JAVA) Create a program that prompts the user for an age input. The program defines the age group of the user. Follow the table below to construct the output for your program. Age Age Group 0 Baby 1-3 Toddler 4-11 Child 12-17 Teenager 18-21 Young Adult 22-64 Adult 65+ Senior Negative Number Invalid Input Sample Input Enter an age: 18 Sample Output: You are a young adult. Sample Input Enter an age: 29 Sample Output: You are an adult. Sample...
Must be in Java Write a program called showTwos that shows the factors of 2 in...
Must be in Java Write a program called showTwos that shows the factors of 2 in a given positive integer (user input) in the range of [16,128]. For example, if user inputs 7, 18, 68, 120, the correctly working program should print the following: 7 = 7 18 = 2 * 9 68 = 2 * 2 * 17 120 = 2 * 2 * 2 * 15
Write a AL program that will interchange the contents of the two variables.
Assume we have two string variables:  Shakespeare byte 'Brevity is the soul of wit' and Poet byte 'The problem is not in the stars but within ourselves'  Write a AL program that will interchange the contents of the two variables.
Write a Java program where you will set the datatype and variables to find the value...
Write a Java program where you will set the datatype and variables to find the value of following expression. The Values of the variables to be used are given below a) 101 + 0) / 3 b) 3.0e-6 * 10000000.1 c) true && true d) false && true e) (false && false) || (true && true) f) (false || false) && (true && true)
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance...
JAVA CODE Write two definitions of a boolean method called equals(). The method compares the instance variables of the class for equality. One is in the Purchase class and the other is a static method of the main. Give sample calls for each. public class PurchaseDemo { public static void main(String[] args) { Purchase oneSale = new Purchase(); oneSale.readInput(); oneSale.writeOutput(); System.out.println("Cost each $" + oneSale.getUnitCost()); System.out.println("Total cost $" + oneSale.getTotalCost()); } } import java.util.Scanner; /**    Class for the purchase...
A JAVA program that will read a boolean matrix corresponding to a relation R and output...
A JAVA program that will read a boolean matrix corresponding to a relation R and output whether R is Reflexive, Symmetric, Anti-Symmetric and/or Transitive. Input to the program will be the size n of an n x n boolean matrix followed by the matrix elements. Document your program nicely. NOTE: The program must output a reason in the case that an input relation fails to have a certain property.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT