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

Java codes for the following problems Problem 1 Given two numbers, representing

ID: 3585011 • Letter: J

Question

Java codes for the following problems

Problem 1

Given two numbers, representing the width and the height of a right triangle, calculate the perimeter of the triangle (i.e., the sum of the lengths of all three of its sides).

Problem 2

Given two strings of text, calculate their concatenation and return it as a separate string of text. The concatenation of two strings is a string that contains all of the characters in the first string, followed by all of the characters in the second. For example, the concatenation of boo and lean is boolean.

(Whenever these problems refer to a "string of text," I'm referring to a sequence of individual characters. Choose an implementation that is appropriate for the language you chose. For example, in Java, you would use a String; in C++, you might use a std::string or a char*; and so on.)

Problem 3

Separately read two pieces of input from a user, which are intended to be both a name and an age (in years). Afterward, print the output Your name is XXX and you are YYY years old, where XXX is the name and YYY is the number of years old. So, for example, if the name was given as Boo and the age was given as 11, you would print Your name is Boo and you are 11 years old. Note, too, that you should finish that sentence with 1 year old instead of 1 years old if the age is 1.

(Whenever these problems refer to reading input from a user, you'll need to handle that in a way that is appropriate for the language you chose. For example, in Java, you might use a Scanner; in C++, you might use std::cin; in C, you might use scanf; and so on.)

Problem 4

Given a list of numbers, compute its sum.

(Whenever these problems refer to a "list", I'm referring to a sequence that may have an arbitrary number of values in it. Choose an implementation that is appropriate for the language you chose. For example, in Java, you might use an ArrayList or an array; in C++, you might use a std::vector or an array; and so on.)

Problem 5

Given a list containing strings of text, determine the longest string (i.e., the string that contains the largest number of characters).

Problem 6

Read input from a user that is intended to be a consist of a license number (such as a driver's license number). If the input is not a valid license number, then ask again, and continue asking until the input is valid.

We'll say, for the purposes of this problem, that a license number must follow these rules in order to be valid:

It must consist only of uppercase letters and digits; anything else (including spaces) would make it invalid.

The first and last characters must be uppercase letters.

There must be at least one digit (and any number of uppercase letters) in between.

By these rules, the following are all valid license numbers: A123B, ABC1DEF, A1B2C3D4E. These would all be invalid: A123, 123B, ABCDEF, A1B2C3D4.

Explanation / Answer

Problem 1:

public class Perimeter {

void perimeter(double width,double height)

{

double value=(width*width)+(height*height);

double hypotenuse=Math.sqrt(value); // calculating the other side value

double perimeter=width+height+hypotenuse; // calculating perimerter of triangle

System.out.println("perimeter of triangle is"+perimeter); // printing the perimeter vale to console

}

public static void main(String[] args) {

Perimeter p=new Perimeter(); // creating the object in order to call the perimeter function

p.perimeter(3, 4); // calling perimeter function to evaluate perimeter

}

}

problem 2:

public class StringConcat {

String concat(String str1,String str2)

{

String concatString=str1.concat(str2); // concatenating the two strings and storing it in another variable

return concatString; // returning the concatenated string

}

public static void main(String[] args) {

StringConcat p=new StringConcat(); //creating object for the class that has concatenating utility function

System.out.println("concatenated String is="+p.concat("boo", "lean")); // printing the concatenated String

}

}

ouput:concatenated String is=boolean

Problem:

import java.util.Scanner;

public class NameandAge {

Scanner sc=new Scanner(System.in);

void print() // function to print name and age

{

System.out.println("enter name");

String name=sc.next();

System.out.println("enter age");

int age=sc.nextInt();

System.out.println("Your name is "+name+" and you are "+age+" years old");

}

public static void main(String[] args) {

NameandAge p=new NameandAge(); // creating the object for the class that has printing name and age utility function

p.print(); // function call to print name and age

}

}

output:

enter name

boo

enter age

11

Your name is booand you are11years old

problem 4:

import java.util.ArrayList;

import java.util.Scanner;

public class ListSum {

void sum(ArrayList<Integer> list) // function to calculate the sum of elements of list

{

int sum=0;

for(Integer ele:list)

{

sum+=ele.intValue(); // adding elements one by one to the sum

}

System.out.println("the sum of elements in list is = "+sum);

}

public static void main(String[] args) {

ArrayList<Integer> al=new ArrayList<Integer>();

Scanner sc=new Scanner(System.in);

System.out.println("enter how many elemsnts you want to enter");

int size=sc.nextInt();

for(int i=0;i<size;i++) //loop for reading elements in to list

{

System.out.println("enter element to be inserted");

al.add(sc.nextInt());

}

ListSum ls=new ListSum(); // creating object for the class that has utility function for calculating the list sum

ls.sum(al); //calling the utility function

}

}

output:

enter how many elemsnts you want to enter

3

enter element to be inserted

20

enter element to be inserted

30

enter element to be inserted

40

the sum of elements in list is = 90

problem 5:

import java.util.ArrayList;

import java.util.Scanner;

public class LongString {

void longestString(ArrayList<String> list) //function for calculating the longest string in the list

{

String longestStr = "";

int length=0;

for(String ele:list)

{

longestStr=length<ele.length()?ele:longestStr; //condition for storing the next longest string in the list

length=length>ele.length()?length:ele.length(); // condition for storing length the next longest string in the list for futher comparisions

}

System.out.println("longest string is = "+longestStr);

}

public static void main(String[] args) {

ArrayList<String> al=new ArrayList<String>();

Scanner sc=new Scanner(System.in);

System.out.println("enter how many elemsnts you want to enter");

int size=sc.nextInt();

for(int i=0;i<size;i++) // loop for reading elements into list

{

System.out.println("enter element to be inserted");

al.add(sc.next());

}

LongString ls=new LongString();// creating object for the class that has utility function to find the longest string in the list

ls.longestString(al);//calling the utility function

}

}

ouput:

enter how many elemsnts you want to enter

3

enter element to be inserted

boo

enter element to be inserted

bool

enter element to be inserted

boolean

longest string is = boolean

problem 6:

import java.util.ArrayList;

import java.util.Scanner;

public class License {

boolean licenseValidation(String licenseNo) // function to check whether license is valid or not

{

int digitCount=0;

if(!Character.isUpperCase(licenseNo.charAt(0)) || !Character.isUpperCase(licenseNo.charAt(licenseNo.length()-1))) //condition for checking whether the first and last digits of the entered license number is capital or not

return false; //if condition fails returning false

else

{

for(int i=1;i<licenseNo.length()-1;i++)

{

if(Character.isUpperCase(licenseNo.charAt(i))) //condition to check remiang all the characters are capital or not

{

continue;

}

else if(Character.isDigit(licenseNo.charAt(i))) //condition to check the digit count

{

digitCount++;

continue;

}

else

return false;

}

if(digitCount>0)

{

System.out.println("license is valid");

return true;

}

else

return false;

}

}

public static void main(String[] args) {

ArrayList<String> al=new ArrayList<String>();

Scanner sc=new Scanner(System.in);

System.out.println("Enter license number");

String licno=sc.next();

License ls=new License();

boolean flag=ls.licenseValidation(licno);

do // do while loop for checking the license validation until the user enters the valid license number

{

if(flag)

System.out.println("license is valid");

else

{

System.out.println("license is invalid please enter valid license number");

licno=sc.next();

flag=ls.licenseValidation(licno);

}

}while(!flag);

}

}

output:

Enter license number

A123

license is invalid please enter valid license number

123B

license is invalid please enter valid license number

ABCDEF

license is invalid please enter valid license number

A1B2C3D4

license is invalid please enter valid license number

A123B

license is valid

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