Java Assignment - Program 8 (two classes) Follow the directions below to submit
ID: 3592940 • Letter: J
Question
Java Assignment - Program 8 (two classes)
Follow the directions below to submit Assignment 8:
1. You will create a Java program that will consist of two classes. One class will be the AnyString class. This class will contain data and code to manipulate the data. The second class will be the TestAnyStringClass. This class will create objects of the AnyString class and test its methods.
2. The AnyString class should contain the following:
a. An single object/instance variable of type String.
b. A constructor method that will accept a single String value and uses it to set the object/instance variable.
c .An object/instance method getString() that accepts no argument and returns the object/instance variable value.
d. An object/instance method lowercase() that accepts no arguments and returns a String value. The method will return the lowercase value of the object/instance variable.
e. An object/instance method uppercase() that accepts no arguments and returns the uppercase value of the object/instance variable.
f. An object/instance method getLength() that accepts no arguments and returns the length of the object/instance variable.
3. The TestAnyStringClass should contain the following:
a. You should input a string.
b. Create an object of the AnyString class.
c. Call all the methods in the AnyString class, verify that they work, and display the results.
Explanation / Answer
AnyString .java
public class AnyString {
// Declaring instance variables
private String str;
// Parameterized constructor
public AnyString(String str) {
super();
this.str = str;
}
// This Method will return the String
public String getStr() {
return str;
}
// This method will convert the string to lower case
public String lowercase() {
return str.toLowerCase();
}
// This method will convert the string to upper case
public String uppercase() {
return str.toUpperCase();
}
// This method will find the length of the String
public int getLength() {
return str.length();
}
}
__________________
TestAnyStringClass.java
import java.util.Scanner;
public class TestAnyStringClass {
public static void main(String[] args) {
//Declaring variables
String str;
/*
* Creating an Scanner class object which is used to get the inputs
* entered by the user
*/
Scanner sc = new Scanner(System.in);
//Getting the input entered by the user
System.out.print("Enter a String :");
str = sc.nextLine();
//Creating an Instance of AnyString class object
AnyString as = new AnyString(str);
//Calling the Methods on the AnyString class
System.out.println(" You Entered String :" + as.getStr());
System.out.println("After Converting " + str + " to LowerCase :" + as.lowercase());
System.out.println("After Converting " + str + " to UpperCase :" + as.uppercase());
System.out.println("The Length of the String is :" + as.getLength());
}
}
______________________
Output:
Enter a String :Sachin Tendulkar
You Entered String :Sachin Tendulkar
After Converting Sachin Tendulkar to LowerCase :sachin tendulkar
After Converting Sachin Tendulkar to UpperCase :SACHIN TENDULKAR
The Length of the String is :16
_____________Could you rate me well.Plz .Thank You
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.