In: Computer Science
I. Consider the condition (x == y). How is this handled if x and
y are primitive types? How is this handled if x and y are
objects?
II. For the program to get a name interactively a Scanner object
must be instantiated. Write the Java statement to do this.
III. Write a statement using a Scanner method to get the first name
interactively.
IV. Write a method to extract the initial (first letter) from the
first name.
V. Write a statement using a method to guarantee that the initial
(first letter) will be a capital letter.
Java programming
I. if x and y are primitive types and condition x==y is applied:
then, it will compare the values stored in them if they are same then it will return true else false.. primitive variables are created in stack.
For example:
1.
int x=5, y=5;
System.out.print(x==y);
OUTPUT would be true.. Since, the value stored in x and y is 5 which is same..
2. int x=5, y=4;
System.out.print(x==y);
OUTPUT would be false.. Since, the value stored in x is 5 and y is 4 which is different..
In case of two objects are compared using "==" operator:
then, the references of two objects are compared.
When an object is created a memory address in heap memory is assigned to it. So, this "==" operator compares the references of two objects.
And every object has a different memory address assigned to it.. So, "==" will only gives true when you compare an object to itself.
To check equality of two objects we have to check the states inside them.. which we can do with a method called equals().
For example:
String in java is considered as object not primitive.
So,
String s1=new String("A");
String s2=new String("A");
if compared with "==":
s1==s2 : it will return false.. because they have different memory address.
but, if compared with equals() method i.e.
s1.equals(s2); it will return true.. because this method compares state of object.
EXAMPLE:
SOURCE CODE:
import java.util.*;
import java.lang.*;
import java.io.*;
class Check
{
public static void main (String[] args) throws
java.lang.Exception
{
String s1=new String("A");
String s2=new String("A");
System.out.print("if == operator is used: ");
System.out.println(s1==s2);
System.out.print("if equals() method is used: ");
System.out.println(s1.equals(s2));
}
}
OUTPUT WOULD BE:
II. To get a name interactively a Scanner object is instantiated this way:
Scanner input=new Scanner(System.in);
Syntax: Scanner <any name you want to give to your Scanner object. Here, I have taken the name "input">=new Scanner(System.in); //Same as object Creation..
Please note: Before using this statement please ensure that you have imported java.util.Scanner:
import java.util.Scanner;
III. To get the first name interactively using Scanner method:
we write: String s=input.next();
Detailed example is below:
SOURCE CODE:
import java.util.Scanner;
class First
{
public static void main (String[] args) throws
java.lang.Exception
{
Scanner input=new Scanner(System.in);
System.out.println("Enter first name: ");
//since name will must be a String, We will declare a String object and So that it can store the user entered..
String s=input.next();
System.out.println(s);
}
}
IV. To extract the initial (first letter) from the first name:
SOURCE CODE:
import java.util.Scanner;
class FirstLetter
{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter first name: ");
//since name will must be a String, We will declare a String object and So that it can store the user entered..
String s=input.next();
char first=s.charAt(0); //this method gives character at a specific position in a string..
System.out.println(first);
}
}
OUTPUT:
V. A statement using a method to guarantee that the initial (first letter) will be a capital letter.
Character.toUpperCase(str.charAt(0)); this is used to upper case first letter //str is the name of string.
Lets see an example:
SOURCE CODE:
import java.util.Scanner;
class CapitalFirst
{
public static void main (String[] args)
{
Scanner input=new Scanner(System.in);
System.out.println("Enter first name: ");
String s=input.next();
//since name will must be a String, We will declare a String object and So that it can store the String entered by user..
System.out.println(change(s));//method calling..by passing user entered String..
}
public static String change(String in)//method
declaration..
{
char s1=Character.toUpperCase(in.charAt(0));
String ans=s1+ in.substring(1); //s.substring(1) will give String
in from position 1..
return ans;
}
}
OUTPUT:
NOTE: I have mentioned the comments wherever neccessary in the codes.. Please save them with their respective class names with extension ".java ". Then, compile and run..
I. Save the example given with "Check.java".
III. Save the example given with "First.java". This example takes input the first name from user interactively using Scanner method.
IV. Save the example given with "FirstLetter.java".This example extracts the initial (first letter) from the first name.
V. Save the example given with "CapitalFirst.java".This example uses a method to guarantee that the initial (first letter) will be a capital letter.
HOPE IT HELPS..!!!!
FOR ANY QUERY OR PROBLEM PLEASE FEEL FREE TO COMMENT..
THANKS.