1) What is the purpose of the new operator? 2) Define the terms arguments and pa
ID: 646853 • Letter: 1
Question
1) What is the purpose of the new operator? 2) Define the terms arguments and parameters. How are they different? 3) Write a method called Greeting that displays a personalized greeting given a first name. 4) Write a Java method named tongueTwister that prints the phrase How much wood could a woodchuck chuck if a woodchuck could chuck wood? 5) Discuss the public and private modifiers in context of methods and instance variables. 6) Write a Java class (and main program) that uses the console window to prompt the user for total miles driven and average speed. Include methods that calculate the estimated time to reach the destination, echo the information back to the user, and provide a cost estimate for the miles entered that uses a cost of $2.25 per gallon of gas. (Refer to display 4.6 from the chapter for further guidance).Explanation / Answer
1.
new operator is use foe object of class,or instance . use for dynamic memory allocation means run tume allocate the memory
example abc a = new abc() // with the help of new operator create the object or insantce of class
2. parameter : when we declare a function we define parameter.The term parameter use for any declaration within the parentheses following the method/function name in a method/function declaration or definition;
example void function(int a,int b);//define paprameter
argument:
when we call a function we define parameter.The term argument use for any calling within the parentheses following the method/function name in a method/function calling
example fun(67,89) // argument at the time of call
3. void greeting(String Fname,String lastname)
{
System.out.println("greeting welcome"+Fname);
}
void tongueTwister()
{
System.out.println("How much wood could a woodchunk chuck if a wood chunk could wood chunk");
}
5. public and private
public modifier : if variable or method mark as public that can be access in different class or different package means anyhwere
class abc
{
public int salary; // this variable access in different class and different package
}
private modifier: if variable or method marks as private that can be access inside the same class
class abc
{
private int salary;} this variable can access inside the same class
6.
import java.util.*;
class abc
{
public static void main(String ar[])
{
System.out.println("enter speed and milage");
abc a= new abc();
Scanner sc= new Scanner(System.in);
int speed=sc.nextInt();
int totalmil=sc.nextInt();
a.reachdesti(totalmil,speed);
}
void reachdesti(int total,int spe)
{
int totalcal=total/spe;
System.out.println("time require to reach destination "+totalcal);
//1 gallon =10 miles
int totalgallon= total/10;
double costest=totalgallon*2.5;
System.out.println("total estimate cost"+costest);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.