If-Else Statements and DeMorgan's Law
Scanner input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter something");
String something = input.nextLine(); // Read user input
System.out.println(something);
boolean period = false;
if (something.contains(".")){
period = true;
}
boolean a = false;
if (something.contains("a")){
a = true;
}
boolean b = false;
if (something.contains("b")){
b = true;
}
boolean five_plus = false;
if(something.length() >= 5){
five_plus = true;
//something has 5 or more letters
// checking an inequality
System.out.println("something has 5 or more letters");
}
else{
//something does not contain more than 5 letters.
System.out.print("something does not contain more than 5 letters. ");
}
if(five_plus && !period ){
//something has more than 5 letters AND doesn't contain a period
//and operator and de morgan's law
System.out.println("something has more than 5 letters AND doesn't contain a period");
}
else{
//something has less than 5 letters. cannot say for sure if it has a period
System.out.println("something has less than 5 letters. cannot say for sure if it has a period");
}
if(a || b ){
// something contains the letter a OR contains the letter b
// or operator
System.out.println("something contains the letter a OR contains the letter b");
}
else{
System.out.println("something doesn't contain the letter a OR contain the letter b");
}
if ((five_plus && !period) && (a || b)) {
//something has more that 5 letters and doesn't contain a period AND something has either the letter a or the letter b
// combination of and and or operators
System.out.println("something has more that 5 letters and doesn't contain a period AND something has either the letter a or the letter b");
}
else{
//something has less that 5 letters. cannot say for sure if something contains a or b or a period
System.out.println("something has less that 5 letters. cannot say for sure if something contains a or b or a period");
}
if (! ((!five_plus || b) || (a && period) )) { //DeMorgan's law with and and or operators
//De Morgan's law with complex expressions: if something has 5+ letters, and/or doesn't have a 'b' and/or doesn't have an 'a' AND has a period.
System.out.println("De Morgan's law with complex expressions: if something has 5+ letters, and/or doesn't have a 'b' and/or doesn't have an 'a' AND has a period.");
}
else{
//De Morgan's law with code, if it doesn't work
System.out.println("De Morgan's law with code, if it doesn't work");
}
Scanner input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter something");
String something = input.nextLine(); // Read user input
System.out.println(something);
boolean period = false;
if (something.contains(".")){
period = true;
}
boolean a = false;
if (something.contains("a")){
a = true;
}
boolean b = false;
if (something.contains("b")){
b = true;
}
boolean five_plus = false;
if(something.length() >= 5){
five_plus = true;
//something has 5 or more letters
// checking an inequality
System.out.println("something has 5 or more letters");
}
else{
//something does not contain more than 5 letters.
System.out.print("something does not contain more than 5 letters. ");
}
if(five_plus && !period ){
//something has more than 5 letters AND doesn't contain a period
//and operator and de morgan's law
System.out.println("something has more than 5 letters AND doesn't contain a period");
}
else{
//something has less than 5 letters. cannot say for sure if it has a period
System.out.println("something has less than 5 letters. cannot say for sure if it has a period");
}
if(a || b ){
// something contains the letter a OR contains the letter b
// or operator
System.out.println("something contains the letter a OR contains the letter b");
}
else{
System.out.println("something doesn't contain the letter a OR contain the letter b");
}
if ((five_plus && !period) && (a || b)) {
//something has more that 5 letters and doesn't contain a period AND something has either the letter a or the letter b
// combination of and and or operators
System.out.println("something has more that 5 letters and doesn't contain a period AND something has either the letter a or the letter b");
}
else{
//something has less that 5 letters. cannot say for sure if something contains a or b or a period
System.out.println("something has less that 5 letters. cannot say for sure if something contains a or b or a period");
}
if (! ((!five_plus || b) || (a && period) )) { //DeMorgan's law with and and or operators
//De Morgan's law with complex expressions: if something has 5+ letters, and/or doesn't have a 'b' and/or doesn't have an 'a' AND has a period.
System.out.println("De Morgan's law with complex expressions: if something has 5+ letters, and/or doesn't have a 'b' and/or doesn't have an 'a' AND has a period.");
}
else{
//De Morgan's law with code, if it doesn't work
System.out.println("De Morgan's law with code, if it doesn't work");
}
Scanner input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter something");
String something = input.nextLine(); // Read user input
System.out.println(something);
boolean period = false;
if (something.contains(".")){
period = true;
}
boolean a = false;
if (something.contains("a")){
a = true;
}
boolean b = false;
if (something.contains("b")){
b = true;
}
boolean five_plus = false;
Integer cases = 0;
if(something.length() >= 5){
five_plus = true;
cases = 1;
}
else{
cases =2;
}
switch (cases) {
case 1:
System.out.println("something has 5 or more letters");
break;
case 2:
System.out.print("something does not contain more than 5 letters. ");
break;
}
if(five_plus && !period ){
cases = 3;
}
else{
cases = 4;
}
switch (cases){
case 3:
System.out.println("something has more than 5 letters AND doesn't contain a period");
break;
case 4:
System.out.println("something has less than 5 letters. cannot say for sure if it has a period");
break;
}
if(a || b ){
cases = 5;
}
else{
cases = 6;
}
switch (cases){
case 5:
System.out.println("something contains the letter a OR contains the letter b");
break;
case 6:
System.out.println("something doesn't contain the letter a OR contain the letter b");
break;
}
if ((five_plus && !period) && (a || b)) {
cases = 7;
}
else{
cases = 8;
}
switch (cases){
case 7:
System.out.println("something has more that 5 letters and doesn't contain a period AND something has either the letter a or the letter b");
break;
case 8:
System.out.println("something has less that 5 letters. cannot say for sure if something contains a or b or a period");
break;
}
if (! ((!five_plus || b) || (a && period) )) { //DeMorgan's law
cases = 9;
}
else{
cases = 10;
}
switch (cases) {
case 9:
System.out.println("De Morgan's law with complex expressions: if something has 5+ letters, and/or doesn't have a 'b' and/or doesn't have an 'a' AND has a period.");
break;
case 10:
System.out.println("De Morgan's law with code, if it doesn't work");
break;
}
Scanner input = new Scanner(System.in); // Create a Scanner object
System.out.println("Enter something");
String something = input.nextLine(); // Read user input
System.out.println(something);
boolean period = false;
if (something.contains(".")){
period = true;
}
boolean a = false;
if (something.contains("a")){
a = true;
}
boolean b = false;
if (something.contains("b")){
b = true;
}
boolean five_plus = false;
Integer cases = 0;
if(something.length() >= 5){
five_plus = true;
cases = 1;
}
else{
cases =2;
}
switch (cases) {
case 1:
System.out.println("something has 5 or more letters");
break;
case 2:
System.out.print("something does not contain more than 5 letters. ");
break;
}
if(five_plus && !period ){
cases = 3;
}
else{
cases = 4;
}
switch (cases){
case 3:
System.out.println("something has more than 5 letters AND doesn't contain a period");
break;
case 4:
System.out.println("something has less than 5 letters. cannot say for sure if it has a period");
break;
}
if(a || b ){
cases = 5;
}
else{
cases = 6;
}
switch (cases){
case 5:
System.out.println("something contains the letter a OR contains the letter b");
break;
case 6:
System.out.println("something doesn't contain the letter a OR contain the letter b");
break;
}
if ((five_plus && !period) && (a || b)) {
cases = 7;
}
else{
cases = 8;
}
switch (cases){
case 7:
System.out.println("something has more that 5 letters and doesn't contain a period AND something has either the letter a or the letter b");
break;
case 8:
System.out.println("something has less that 5 letters. cannot say for sure if something contains a or b or a period");
break;
}
if (! ((!five_plus || b) || (a && period) )) { //DeMorgan's law
cases = 9;
}
else{
cases = 10;
}
switch (cases) {
case 9:
System.out.println("De Morgan's law with complex expressions: if something has 5+ letters, and/or doesn't have a 'b' and/or doesn't have an 'a' AND has a period.");
break;
case 10:
System.out.println("De Morgan's law with code, if it doesn't work");
break;
}