Computer Applications
Write a program by using a class in Java with the following specifications:
Class name — Stringop
Data members:
- String str
Member functions:
- Stringop() — to initialize str with NULL
- void accept() — to input a sentence
- void encode() — to replace and print each character of the string with the second next character in the ASCII table. For example, A with C, B with D and so on
- void print() — to print each word of the String in a separate line
Java
Java Constructors
42 Likes
Answer
import java.util.Scanner;
public class Stringop
{
private String str;
public Stringop() {
str = null;
}
public void accept() {
Scanner in = new Scanner(System.in);
System.out.println("Enter a sentence: ");
str = in.nextLine();
}
public void encode() {
char[] arr = new char[str.length()];
for (int i = 0; i < str.length(); i++) {
arr[i] = (char)(str.charAt(i) + 2);
}
str = new String(arr);
System.out.println("\nEncoded Sentence:");
System.out.println(str);
}
public void print() {
System.out.println("\nPrinting each word on a separate line:");
int s = 0;
while (s < str.length()) {
int e = str.indexOf(' ', s);
if (e == -1)
e = str.length();
System.out.println(str.substring(s, e));
s = e + 1;
}
}
public static void main(String args[]) {
Stringop obj = new Stringop();
obj.accept();
obj.print();
obj.encode();
}
}
Variable Description Table
Program Explanation
Output
Answered By
11 Likes
Related Questions
Define a class Arrange described as below:
Data members/instance variables:
- String str (a word)
- String i
- int p (to store the length of the word)
- char ch;
Member Methods:
- A parameterised constructor to initialize the data member
- To accept the word
- To arrange all the alphabets of word in ascending order of their ASCII values without using the sorting technique
- To display the arranged alphabets.
Write a main method to create an object of the class and call the above member methods.
The population of a country in a particular year can be calculated by:
p*(1+r/100) at the end of year 2000, where p is the initial population and r is the
growth rate.Write a program by using a class to find the population of the country at the end of each year from 2001 to 2007. The Class has the following specifications:
Class name — Population
Data Members — float p,r
Member Methods:
- Population(int a,int b) — Constructor to initialize p and r with a and b respectively.
- void print() — to calculate and print the population of each year from 2001 to 2007.
Write a program by using a class with the following specifications:
Class name — Calculate
Instance variables:
- int num
- int f
- int rev
Member Methods:
- Calculate(int n) — to initialize num with n, f and rev with 0 (zero)
- int prime() — to return 1, if number is prime
- int reverse() — to return reverse of the number
- void display() — to check and print whether the number is a prime palindrome or not
Write a program in Java to find the roots of a quadratic equation ax2+bx+c=0 with the following specifications:
Class name — Quad
Data Members — float a,b,c,d (a,b,c are the co-efficients & d is the discriminant), r1 and r2 are the roots of the equation.
Member Methods:
- quad(int x,int y,int z) — to initialize a=x, b=y, c=z, d=0
- void calculate() — Find d=b2-4ac
If d < 0 then print "Roots not possible" otherwise find and print:
r1 = (-b + √d) / 2a
r2 = (-b - √d) / 2a