Question

Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...

Please show screenshot outputs and fully functional code for the Java programs.

Question 1:

Write a method that computes and returns the area of a square using the following header:

public static double area ( double side)

Write a main method that prompts the user to enter the side of a square, calls the area method then displays the area.

Question 1a:

Write a method that converts miles to kilometers using the following header:

public static double convertToKilometers (double miles)

Write a main method that prompts the user to enter the value of miles, calls the method then displays the value of kilometers.

Question 1b:

Write a method that will replace the spaces in a sentence with dashes (-).

public static String replaceWithDash(String aSentence)

Write a main method that prompts the user to enter a sentence, calls the method then displays the new sentence

Question 1c:

Write 2 methods: one to find the maximum of three integer numbers and the other to find the minimum of three integer numbers

public static int findMax(int n1, int n2, int n3)

public static int findMin(int n1, int n2, int n3)

Write a main method that prompts the user to enter the three values, calls the methods then displays the value of maximum number then the value of the minimum number.

Question 1d:

Write a method to generate a random vehicle plate number.

public static String generatePlateNum()

Write a main method that will call the method and then print the vehicle plate number

Question 1e:

Write a method to calculate the sum of 10 integer numbers. The method will ask the user to enter the 10 numbers and calculate the sum.

public static int calcSum()

Write a test program (main method) that will call the method and then print the sum.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Q1.

import java.util.*;
import java.lang.*;
import java.io.*;
class Q1
{
   public static double area(double side)
   {
       double area;
       area=side*side;
       return area;
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("enter the side of the square:");
       double side=Double.parseDouble(x.readLine());
       System.out.println("area of the square is:"+area(side));
   }
}

Q1a.

import java.util.*;
import java.lang.*;
import java.io.*;
class Q2
{
   public static double convertToKilometers(double miles)
   {
       double km;
       km=1.60934*miles;
       return km;
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("enter the value of miles:");
       double miles=Double.parseDouble(x.readLine());
       System.out.println("value of kilometers:"+convertToKilometers(miles));
   }
}

Q1b.

import java.util.*;
import java.lang.*;
import java.io.*;
class Q3
{
   public static String replaceWithDash(String aSentence)
   {
       String s=" ";
       int i;
       for(i=0;i<aSentence.length();i++)
       {
           if(aSentence.charAt(i)==' ') //checking if the char at ith index is a blank space
           s=s+"-"; //here we are adding - instead of blank spaces in the new string
           else //if not blank space we add the characet to the new string
           s=s+aSentence.charAt(i); //here we are adding the character present at ith index
       }
       return s;
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       Scanner x=new Scanner(System.in);
       System.out.println("enter a sentence:");
       String aSentence=x.nextLine();
       System.out.println("the new sentence:"+replaceWithDash(aSentence));
   }
}

Q1c.

import java.util.*;
import java.lang.*;
import java.io.*;
class Q4
{
   public static int findMax(int n1, int n2, int n3)
   {
       return (n1>n2)?((n1>n3)?n1:n3):((n2>n3)?n2:n3); //using nested ternary operator to find max
   }
   public static int findMin(int n1, int n2, int n3)
   {
       return (n1<n2)?((n1<n3)?n1:n3):((n2<n3)?n2:n3); //using nested ternary operator to find min
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("enter the 1st integer:");
       int n1=Integer.parseInt(x.readLine());
       System.out.println("enter the 2nd integer:");
       int n2=Integer.parseInt(x.readLine());
       System.out.println("enter the 3rd integer:");
       int n3=Integer.parseInt(x.readLine());
       System.out.println("value of maximum number:"+findMax(n1,n2,n3));
       System.out.println("value of minimum number:"+findMin(n1,n2,n3));
   }
}

Q1d.

import java.util.*;
import java.lang.*;
import java.io.*;
class Q5
{
   public static String generatePlateNum()
   {
       StringBuilder platenum = new StringBuilder();
       for (int i = 0; i < 3; i++)
       {
           char alpha= (char)(Math.random()* 26 + 'A');
           platenum.append(alpha);
          
       }
       for (int i = 0; i < 4; i++)
       {
           char num=(char)(Math.random()*10+'0');
           platenum.append(num);
       }
       String s=" ";
           for (int i = 0; i < 7; i++)
           {
               s=s+platenum.charAt(i);
           }
           return s;
   }
public static void main (String[] args) throws java.lang.Exception
   {
       System.out.println("the random vehicle plate number is:"+generatePlateNum());
   }
}

Q1e.

import java.util.*;
import java.lang.*;
import java.io.*;
class Q6
{
   static int arr[]=new int[10];
   Q6()
   {
       int i;
       for(i=0;i<10;i++)
       {
           arr[i]=0;
       }
   }
   public static int calcSum()
   {
       int sum=0,i;
       for(i=0;i<10;i++)
       {
           sum=sum+arr[i];
       }
       return sum;
   }
   public static void main (String[] args) throws java.lang.Exception
   {
       int i;
       Q6 a=new Q6(); //creating object of the class Q6
       BufferedReader x=new BufferedReader(new InputStreamReader(System.in));
       System.out.println("enter the array:");
       for(i=0;i<10;i++)
       {
       System.out.println("enter the an integer:");
       a.arr[i]=Integer.parseInt(x.readLine());
       }
       System.out.println("the sumof the 10 integer numbers is:"+a.calcSum());
   }
}

Add a comment
Know the answer?
Add Answer to:
Please show screenshot outputs and fully functional code for the Java programs. Question 1: Write a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that a...

    I need java code for the following problem. Lab 7: Methods 1. Write a Java program called Numbers that calls the following methods and displays the returned value: Write a method called cubelt that accepts one integer parameter and returns the value raised to the third power as an integer. o Write a method called randominRange that accepts two integer parameters representing a range. The method returns a random integer in the specified range inclusive. 2. o Write a method...

  • I need to Write a test program that prompts the user to enter 10 double values...

    I need to Write a test program that prompts the user to enter 10 double values into an array, calls a method to calculate the average of the numbers, and displays the average. Next, write a method that returns the lowest value in the array and display the lowest number. The program should then prompt the user to enter 10 integer values into an array, calculates the average, and displays the average. The program should have two overloaded methods to...

  • Write a test program that prompt the user to enter seven numbers, stores them in an...

    Write a test program that prompt the user to enter seven numbers, stores them in an array list and do the following: 1. Displays its elements (numbers) in increasing order by invoking a method with the following header that sorts the array: public static void sort(ArrayList<Integer>list) 2. Write a method that returns and displays the sum of all numbers (elements) of the ArrayList. Using the following header: public static double sum(ArrayList<Integer>list) 3. Write a method that removes the duplicate numbers...

  • This needs too be in java programming language 2 Write a recursive method that parses a...

    This needs too be in java programming language 2 Write a recursive method that parses a hex number as a string into a decimal integer. The method header is: public static int hex. 2 Dec (String hexstring) Write a pensare cam that that prompts the user to enter a hex, string & displays its decimal equivalent. Recalls Anc=1010566-110. z 490 Use the following her values to convert: BAD, BAC98, BabA73

  • Please use public class for java. Thank you. 1. (10 points) Write a method that computes...

    Please use public class for java. Thank you. 1. (10 points) Write a method that computes future investment value at a given interest rate for a specified number of years. The future investment is determined using the formula futurelnvestmentValue numberOfYears 12 investmentAmount X ( monthlyInterestRate) Use the following method header: public static double futurelnvestmentValue( double investmentAmount, double monthlyInterestRate, int years) For example, futureInvestmentValue 10000, 0.05/12, 5) returns 12833.59. Write a test program that prompts the user to enter the investment...

  • 14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a...

    14.8 Prog 8 Overload methods (find avg) Write a program to find the average of a set numbers (assume that the numbers in the set is less than 20) using overload methods. The program first prompts the user to enter a character; if the character is 'I' or 'i', then invokes the appropriate methods to process integer data set; if the character is 'R' or 'r', then invokes the appropriate methods to process real number data set; if the inputted...

  • (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value...

    (Java Programmin): Sum() and max() in ArrayList a. Write a method that returns the maximum value in an ArrayList of integers. The method returns null if the list is null or the list size is 0.          public static Integer max(ArrayList<Integer> list) b. Write a method that returns the sum of all numbers in an ArrayList: public static Integer sum(ArrayList<Integer> list) c. Write a test program that prompts the user to enter a sequence of numbers ending with 0, and invokes...

  • 1. Write a program that prompts the user to enter three integers and display the integers...

    1. Write a program that prompts the user to enter three integers and display the integers in non-decreasing order. You can assume that all numbers are valid. For example: Input Result 140 -5 10 Enter a number: Enter a number: Enter a number: -5, 10, 140 import java.util.Scanner; public class Lab01 { public static void main(String[] args) { Scanner input = new Scanner(System.in);    } } ---------------------------------------------------------------------------------------------------------------------------- 2. Write a program that repeatedly prompts the user for integer values from...

  • Could someone re-write this code so that it first prompts the user to choose an option...

    Could someone re-write this code so that it first prompts the user to choose an option from the calculator (and catches if they enter a string), then prompts user to enter the values, and then shows the answer. Also, could the method for the division be rewritten to catch if the denominator is zero. I have the bulk of the code. I am just having trouble rearranging things. ------ import java.util.*; abstract class CalculatorNumVals { int num1,num2; CalculatorNumVals(int value1,int value2)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT