Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a new Java project using NetBeans, giving it the name Lab4 . In Lab4Part1

ID: 3587796 • Letter: C

Question

Create a new Java project using NetBeans, giving it the name Lab4.

In Lab4Part1 do the following:Write the main method that will test the following methods:

Write the method to convert Celsius to Fahrenheit.

Write the method to convert Fahrenheit to Celsius

Write a method operator(a,b,c) which will receive 2 integers and one of the 5 operators (*,/,+ -, %) as a character and perform the operation and return the integer value . Example operator(2,3,’*’) will return a 6. (assume no division by zero!))

Write the overloaded method operator(a,b,c) which will receive 2 doubles and one of the 4 operators (*,/,+ -) as a character and perform the operation and return the integer value . Example operator(2.1,3,’*’) will return a 63. (assume no division by zero!))

Write the method sumOfDigits(a) which will receive and integer and return the sum of their digits. (This was a lab!!) Example sumOfDigits(941) return 15.

Enter the following code and explain in comments what is happening

public class Lab4Part2 {

    /* The methods below illustrate invoking methods from other methods.

     * Review the code provided, and show the order in which

     * all of the methods are called.

     *

     * Trace the calls by hand yourself, then confirm your answer by

     * running the code.

     *

     * Note that the method calls are preceded by the name of the class.

     * In main, add at least 3 calls to the methods you created

     * from part 1 using any test values you wish.

     */

    public static void a(){

        System.out.println(" a in ");

        System.out.println(" a out ");

    }//a

    public static void b(){

        System.out.println(" b in ");

        Lab4Part2.a();

        System.out.println(" b out ");

    }//b

    public static void c(){

        System.out.println("c in ");

        Lab4Part2.a();

        Lab4Part2.b();

        System.out.println("c out ");

    }//c

    public static void main(String[] args) {

       System.out.println("main in ");

       Lab4Part2.a();

       Lab4Part2.b();

       Lab4Part2.c();

       System.out.println("main out ");

    }//main

}//class

Add another class to your Lab4 project, and name this class Lab4Part3. In this class, create an isAscending method that accepts 3 integer numbers. The method should return true if the numbers are in ascending order. Write a main method that tests your isAscending method. Your main should display the numbers tested along with the result, such as

The numbers 20, 40, and 50 are in ascending order

The numbers 20, 14, and 9 are NOT in ascending order

In your main invoke your method 3 times, once with numbers that are ascending, once with numbers that are not ascending, and once with numbers that are the same. Copy and paste your output.

Make sure your code is readable, indented and spaced.

Explanation / Answer

Lab4Part2.java

import java.util.Scanner;

public class Lab4Part2 {
static Scanner sc = new Scanner(System.in);

/*
* The methods below illustrate invoking methods from other methods. Review
* the code provided, and show the order in which all of the methods are
* called.
*
* Trace the calls by hand yourself, then confirm your answer by running the
* code.
*
* Note that the method calls are preceded by the name of the class. In
* main, add at least 3 calls to the methods you created from part 1 using
* any test values you wish.
*/
public static void celsiusToFahrenheit() {
double celsius;

System.out.print(" Enter Temperature in Celsius C:");
celsius = sc.nextDouble();

// Converting celsius to fahrenheit
double fahrenheit = (9.0 / 5.0) * celsius + 32.0;

System.out.println(celsius + "C is " + fahrenheit + "F");
}

public static void fahrenheitToCelsius() {
double fahrenheit;
double celsius;

System.out.print(" Enter Temperature in Fahrenheit F:");
fahrenheit = sc.nextDouble();

// Converting fahrenheit to celsius
celsius = (5.0 / 9.0) * (fahrenheit - 32.0);


System.out.printf("%.2fF is %.2fC ", fahrenheit, celsius);
}

public static int operator(int num1, int num2, char operator) {
// Based on operator corresponding case will get executed

int result = 0;
switch (operator) {
case '+':
{
result = (num1 + num2);
break;
}
case '-':
{
result = (num1 - num2);
break;
}
case '*':
{

result = (num1 * num2);
break;
}
case '/':
{

result = (num1 / num2);
break;
}
}
return result;
}

public static double operator(double num1, double num2, char operator) {
// Based on operator corresponding case will get executed

double result = 0;
switch (operator) {
case '+':
{
result = (num1 + num2);
break;
}
case '-':
{
result = (num1 - num2);
break;
}
case '*':
{

result = (num1 * num2);
break;
}
case '/':
{

result = (num1 / num2);
break;
}
}
return result;
}

public static int sumOfDigits(int a) {
int t, sum = 0, remainder;
t = a;

while (t != 0) {
remainder = t % 10;
sum = sum + remainder;
t = t / 10;
}

return sum;
}

public static void main(String[] args) {

int a, b;
double c, d;
char operator;

Lab4Part2.celsiusToFahrenheit();
Lab4Part2.fahrenheitToCelsius();
System.out.print("Enter two Integers :");
a = sc.nextInt();
b = sc.nextInt();
System.out.print("Enter Operator :");
operator = sc.next(".").charAt(0);
int res = Lab4Part2.operator(a, b, operator);
System.out.println("The Sum of " + a + " is " + b + " is " + res);

System.out.print("Enter two Double Values :");
c = sc.nextDouble();
d = sc.nextDouble();
System.out.print("Enter Operator :");
operator = sc.next(".").charAt(0);
double resD = Lab4Part2.operator(c, d, operator);
System.out.println("The Sum of " + c + " is " + d + " is " + resD);

System.out.print("Enter an Integer :");
a = sc.nextInt();
int sum = Lab4Part2.sumOfDigits(a);
System.out.println("The Sum of digits of " + a + " is :" + sum);
} // main
} // class

____________________

Output:


Enter Temperature in Celsius C:45
45.0C is 113.0F

Enter Temperature in Fahrenheit F:56
56.00F is 13.33C
Enter two Integers :34 45
Enter Operator :+
The Sum of 34 is 45 is 79
Enter two Double Values :23.3 45.5
Enter Operator :*
The Sum of 23.3 is 45.5 is 1060.15
Enter an Integer :789
The Sum of digits of 789 is :24

__________________

Lab4Part3.java

import java.util.Scanner;

public class Lab4Part3 {

public static void main(String[] args) {

//Declaring variables
int a, b, c;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
boolean bool;

//This for loop will call the method for 3 times
for (int i = 1; i <= 3; i++) {
//Getting the inputs entered by the user
System.out.print("Enter Integer a :");
a = sc.nextInt();
System.out.print("Enter Integer b :");
b = sc.nextInt();
System.out.print("Enter Integer c :");
c = sc.nextInt();

//calling the method by passing the user entered input as arguments
bool = isAscending(a, b, c);
if (bool) {
System.out.println("The numbers " + a + "," + b + ", and " + c + " are in ascending order");
} else {
System.out.println("The numbers " + a + "," + b + ", and " + c + " are NOT in ascending order");
}
}

}

/* This method will check whether the numbers passed
* as arguments are in ascending order or not
*/
private static boolean isAscending(int a, int b, int c) {
if ((a < b && a < c) && (b < c))
return true;
else
return false;
}

}

____________________

Output:

Enter Integer a :20
Enter Integer b :40
Enter Integer c :60
The numbers 20,40, and 60 are in ascending order
Enter Integer a :20
Enter Integer b :14
Enter Integer c :9
The numbers 20,14, and 9 are NOT in ascending order
Enter Integer a :45
Enter Integer b :2
Enter Integer c :89
The numbers 45,2, and 89 are NOT in ascending order

_____________Could you rate me well.Plz .Thank You

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote