Posts

Showing posts from September, 2020

Java Practice Set 4

Image
 Java Practice Set 4 QUESTIONS --- SOLUTIONS/CODE --- package com.company ; import java.util.Scanner ; public class PS4 { public static void main (String[] args) { /* Q1 - What will be the output of this program: int a = 10; if (a=11){ System.out.println("I am 11"); } else{ System.out.println("I am not 11"); } ANSWER - This will simply throw an error because, in if statement, we used '=' as an assignment operator to run the program correctly, we should have use a logical operator '==' */ /* Q2 - Write a program to find out to whether a student is pass or fail; To pass it requires 40% of total marks and 33% marks of each subjects Assume 3 subjects and take marks as an input from the user */ byte s1 , s2 , s3 ; Scanner sc = new Scanner(System. in ) ; System. out .println( "Enter your marks in Physics"...

Java Tutorial 12 --- "Switch Case Statements"

Image
 Java Tutorial 12 --- "Switch Case Statements" DOC ---  CODE --- package com.company ; import java.util.Scanner ; public class Tut12 { public static void main (String[] args) { // Switch Case Statements System. out .println( "Please enter your Number" ) ; Scanner sc = new Scanner(System. in ) ; int number = sc.nextInt() ; switch (number) { case 18 -> System. out .println( "You have won nothing but you have to Pay Double" ) ; case 7 -> System. out .println( "You have won the Double Prize" ) ; case 777 -> System. out .println( "You have won the MEGA Prize" ) ; case 13 -> System. out .println( "You cant win any prizes for one year" ) ; } if (number>= 60 ){ System. out .println( "Congratulations! You have won the Platinum Prize " ) ; } else if (number>= 50 ){ ...

Java Tutorial 11 --- "Relational and Logical Operators"

Image
 Java Tutorial 11 --- "Relational and Logical Operators" DOC ---  CODE ---  package com.company ; public class Tut11 { public static void main (String[] args) { // Relational and Logical Operators System. out .println( "For Logical AND....." ) ; Boolean a = true; Boolean b = true; Boolean c = true; if (a && b && c){ System. out .println( "Y" ) ; } else { System. out .println( "N" ) ; } System. out .println( "For Logical OR....." ) ; Boolean d = true; Boolean e = false; Boolean f = true; if (a && b || c){ System. out .println( "Y" ) ; } else { System. out .println( "N" ) ; } System. out .println( "For logical NOT...." ) ; System. out .println( "Not A is - " + !a) ; Sy...

Java Tutorial 10 --- "If-else Statement in Java"

Image
 Java Tutorial 10 --- "If-else Statement in Java" DOC ---  CODE ---  package com.company ; import java.util.Scanner ; public class Tut10 { public static void main (String[] args) { // If-else Statement in Java System. out .println( "Enter your age --- " ) ; Scanner sc = new Scanner(System. in ) ; // int age = 20; int age = sc.nextInt() ; //Using Input function (Scanner class) if (age>= 18 ){ System. out .println( "Yes boy you can drive." ) ; } else { System. out .println( "No boy you cannot drive, not yet." ) ; } } }

Java Practice Set 3

Image
 Java Practice Set 3 QUESTIONS -  SOLUTIONS/CODE ---  package com.company ; public class PS3 { public static void main (String[] args) { // Q1 - Write a JAVA program to covert a string to lowercase String str = "String" ; str = str.toLowerCase() ; System. out .println(str) ; // Q2 - Write a JAVA program to replace spaces with underscore String text = "Write a JAVA program to replace spaces with underscore" ; text = text.replaceAll( " " , "_" ) ; System. out .println(text) ; /* Q3 - Write a JAVA program to fill in a template which looks like below template = "Dear<|name|>, Thanks a lot" Replace 'name' with someone's name*/ String template = "Dear <|name|>, Thanks a lot" ; template = template.replace( "<|name|>" , "Saswata" ) ; System. out .println(template) ; // Q4 - Write a JAVA progr...

Java Tutorial 9 --- "String Methods"

Image
 Java Tutorial 9 --- "String Methods" Code ---  package com.company ; public class Tut9 { public static void main (String[] args) { // String Methods in Java String name = "Saswata" ; System. out .println(name) ; int value = name.length() ; System. out .println(value) ; String lowercase = name.toLowerCase() ; System. out .println(lowercase) ; String uppercase = name.toUpperCase() ; System. out .println(uppercase) ; String nonTrimmedString = " Saswata " ; System. out .println(nonTrimmedString) ; String trimmedString ; System. out .println(nonTrimmedString.trim()) ; System. out .println(name.substring( 3 )) ; System. out .println(name.substring( 1 , 6 )) ; System. out .println(name.replace( 'a' , 'u' )) ; System. out .println(name.replace( "ata" , "bat" )) ; System. out ...

Java Tutorial 8 --- "Introduction to Strings"

Image
 Java Tutorial 8 --- "Introduction to Strings" CODE --- package com.company ; import java.util.Scanner ; public class Tut8 { public static void main (String[] args) { // Introduction to String // String name = new String("Saswata"); String name = "Saswata" ; // Strings are immutable System. out .print( "My name is " ) ; //print doesn't print a new line after print. System. out .println(name) ; //println does print a new line after print. int a = 5 ; float b = 6.5545f ; System. out .printf( "The value of a is %d and the value of b is %f" , a , b) ; System. out .println() ; System. out .printf( "The value of a is %d and the value of b is %.2f" , a , b) ; System. out .println() ; System. out .printf( "The value of a is %d and the value of b is %10.2f" , a , b) ; System. out .println() ; System. out .print...

JAVA Practice Set 2

JAVA Practice Set 2 QUESTIONS -  Q1 - What will be the result of the following expression --- float f = 7/4 * 9/2. Q2 - Write a JAVA program to encrpyt a grade by adding 8 to it. Decrypt it to show correct           grade. Q3 - Use Comparison Operators to find out whether a given number is greater than the user           entered number or not. Q4 - Write the following expression in a JAVA program --- v^2-u^2/2as. Q5 - Find the value of the following expression --- int x = 7*49/7+35/7 . CODE - package com.company ; import java.util.Scanner ; public class PS2 { public static void main (String[] args) { // Practice Set 2 /* Q1 - What will be the result of the following expression float f = 7/4 * 9/2 */ float f = 7 / 4f * 9 / 2f ; System. out .println(f) ; /* Q2 - Write a JAVA program to encrpyt a grade by adding 8 to it. Decrypt it to show correct grade. */ char grade =...

Java Tutorial 7 --- "Data Type of Expressions & Increment/Decrement Operators"

Java Tutorial 7 --- "Data Type of Expressions & Increment/Decrement Operators" package com.company ; public class Tut7 { public static void main (String[] args) { // Data Type of Expressions & Increment/Decrement Operators /* b+s = int | b = byte s+i = int | s = short l+f = float | i = integer i+f = float | l = long c+i = int | f = float c+s = int | d = double l+d = double | c = character f+d = double | */ // int i = 45 + 5; // System.out.println(i); // float f = 6.5f + 2; // System.out.println(f); // Increment & Decrement Operators int i = 50 ; System. out .println(i++) ; //First use i then increment System. out .println(i) ; System. out .println(++i) ; // First increment i then use Sys...