Question

In: Computer Science

I. Consider the condition (x == y). How is this handled if x and y are...

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

Solutions

Expert Solution

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.


Related Solutions

What is the pre-condition for x, if the post-condition is -30 <= y <= 0 for...
What is the pre-condition for x, if the post-condition is -30 <= y <= 0 for the following program segment? y = x * x - 5 *x - 24
Consider the following utility functions: (i) u(x,y) = x2y (ii) u(x,y) = max{x,y} (iii) u(x,y) =...
Consider the following utility functions: (i) u(x,y) = x2y (ii) u(x,y) = max{x,y} (iii) u(x,y) = √x + y (a) For each example, with prices px = 2 and py = 4 find the expenditure minimising bundle to achieve utility level of 10. (b) Verify, in each case, that if you use the expenditure minimizing amount as income and face the same prices, then the expenditure minimizing bundle will maximize your utility.
a) Let y be the solution of the equation  y ′ = (y/x)+1+(y^2/x^2) satisfying the condition  y (...
a) Let y be the solution of the equation  y ′ = (y/x)+1+(y^2/x^2) satisfying the condition  y ( 1 ) = 0. Find the value of the function  f ( x ) = (y ( x ))/x at  x = e^(pi/4) . b) Let y be the solution of the equation  y ′ = (y/x) − (y^2/x^2) satisfying the condition  y ( 1 ) = 1. Find the value of the function  f ( x ) = x/(y(x)) at  x = e  . c) Let y be the solution...
QUESTION 1 Consider the Boolean function F(x, y) = x + y, how many cells in...
QUESTION 1 Consider the Boolean function F(x, y) = x + y, how many cells in the Kmap representing this function have value of “1”? A. 3 B. 2 C. 4 D. 1 10 points    QUESTION 2 Using Kmap for simplification, we can select multiple smaller groups (instead of a larger group) as long as all “1” are selected. A. False B. True 10 points    QUESTION 3 In Kmap representation, how many values of “0” and “1” two...
1) Let y be the solution of the equation y ′ = 4(x^4)*sin(x^4) satisfying the condition...
1) Let y be the solution of the equation y ′ = 4(x^4)*sin(x^4) satisfying the condition y ( 0 ) = − 1. Find y ( pi^1/3 ). 2) Find the largest value of the parameter r for which the function y = e^(rx) is a solution of the equation y ″ − 14 y ′ + 28 y = 0. 3) Let y ′ = − 4x^2*e^(-x^4) and let y ( 0 ) = 1. Find ln ⁡ (...
Consider a sheet of 2d graphene in the x-y plane. The 2d sheet of graphene i...
Consider a sheet of 2d graphene in the x-y plane. The 2d sheet of graphene i then sut into a ribbon in the x direction. The energy becomes quantizes in the y direction. calcualte the  charge carrier energies E as a function of kx and ky.
Consider the following function: f (x , y , z ) = x 2 + y...
Consider the following function: f (x , y , z ) = x 2 + y 2 + z 2 − x y − y z + x + z (a) This function has one critical point. Find it. (b) Compute the Hessian of f , and use it to determine whether the critical point is a local man, local min, or neither? (c) Is the critical point a global max, global min, or neither? Justify your answer.
Consider walks in the X-Y plane where each step is R: (x, y)→(x+1, y) or U:...
Consider walks in the X-Y plane where each step is R: (x, y)→(x+1, y) or U: (x, y)→(x, y+a), with a a positive integer. There are five walks that contain a point on the line x + y = 2, namely:  RR, RU1, U1R, U1U1, and U2. Let a_n denote the number of walks that contain a point on the line x + y = n (so a_2 = 5). Show that a_n = F_{2n}, where F_n are the Fibonacci numbers...
Consider the following production function: f(x,y)=x+y^0.5. If the input prices of x and y are wx...
Consider the following production function: f(x,y)=x+y^0.5. If the input prices of x and y are wx and wy respectively, then find out the combination of x and y that minimizes cost in order to produce output level q. Also find the cost function.
Consider the functionT:R3→R3defined byT(x,y,z) = (3x,x−y,2x+y+z).(i) Prove thatTis a linear transformation. (T’nin bir lineer d ̈on...
Consider the functionT:R3→R3defined byT(x,y,z) = (3x,x−y,2x+y+z).(i) Prove thatTis a linear transformation. (T’nin bir lineer d ̈on ̈u ̧s ̈um oldu ̆gunu g ̈osteriniz)(ii) Find the representing matrix ofTrelative to the basisβ={α1= (1,0,0),α2= (1,1,0),α3=(1,1,1)}ofR3.(R3 ̈unβ={α1= (1,0,0),α2= (1,1,0),α3= (1,1,1)}tabanına g ̈oreTnin matris g ̈osterimini bulunuz.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT