Java | JSP | JSF Answers

Questions: 4 418

Answers by our Experts: 3 943

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Search & Filtering

import java.util.*;



class App {



public static void main(String[] args) {

Scanner in = new Scanner(System.in);

System.out.println("Enter a String: ");

String words[] = in.nextLine().split(" ");

for (int i = 0; i < words.length; i++) {

if (i % 2 == 0) {

System.out.print(words[i].substring(0, 1) + ". ");

} else {

System.out.print(words[i] + " ");

}

}



in.close();

}

}

Do this program without using array and split


Write two subclasses of Shape called Circle and Rectangle, as shown in the class diagram.

a) The Circle class contains:

· An instance variable radius (double).

· Three constructors as shown. The no-arg constructor initializes the radius to 1.0.

· Getter and setter for the instance variable radius.

· Methods getArea() and getPerimeter().

· Override the inherited toString() method, to return "A Circle with radius=xxx, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.

b) The Rectangle class contains:

· Two instance variables width (double) and length (double).

· Three constructors as shown. The no-arg constructor initializes the width and length to 1.0.

· Getter and setter for all the instance variables.

· Methods getArea() and getPerimeter().

· Override the inherited toString() method, to return "A Rectangle with width=xxx and length=zzz, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.


a) Write a class called Square, as a subclass of Rectangle. Convince yourself that Square can be modeled as a subclass of Rectangle. Square has no instance variable, but inherits the instance variables width and length from its superclass Rectangle.

· Provide the appropriate constructors (as shown in the class diagram). Hint:

public Square(double side) {

super(side, side); // Call superclass Rectangle(double, double)

}

· Override the toString() method to return "A Square with side=xxx, which is a subclass of yyy", where yyy is the output of the toString() method from the superclass.

· Do you need to override the getArea() and getPerimeter()? Try them out.

· Override the setLength() and setWidth() to change both the width and length, so as to maintain the square geometry.



a) Write a superclass called Shape (as shown in the class diagram), which contains:

· Two instance variables color (String) and filled (boolean).

· Two constructors: a no-arg (no-argument) constructor that initializes the color to "green" and filled to true, and a constructor that initializes the color and filled to the given values.

· Getter and setter for all the instance variables. By convention, the getter for a boolean variable xxx is called isXXX() (instead of getXxx() for all the other types).

· A toString() method that returns "A Shape with color of xxx and filled/Not filled".

Write a test program to test all the methods defined in Shape.


Bob was quite bored at home and was not telling his mom work in the kitchen. To keep him busy, him mom gave him N integers and asked him to find the total number of pairs of integers such that both the numbers are duplicates of each other. Two numbers are said to be duplicates if they follow the following rule:




. When the numbers are written in the binary forms, the sum of their bits is equal.




Bob is not very good at problem solving. Hence, he approaches you for help. As Bob's friend, you must help him accomplish the task assigned by his mom.

Write a program to take a String which is in decimal form as input then display how many int values were found, how many decimal values were found. Then display the integer part and decimal part separately and their addition with appropriate heading.

Hint: Use java.io package and parseInt()


Write a program to take a String name as input then display the signature using substring().

Example:

Enter a String

Chandra Bahadur Dangi Ailen Coq

C. Bahadur D. Ailen C.


Create a class called Account with member variable balance. Create three methods one for Depositing Money, the second for withdrawing money and the third for checking balance. In addition to the three classes, add a constructor to initialize the balance variable. Have the initial balance of the account set to 712,983.88. Have a user deposit any amount and prompt them if they wish to withdraw any amount. If not, print out the balance of the account.


Write a program to take a name as input then display the signature as the first letter of each word with dot.

Enter a String

Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr

H.B.W.S.


1. Trips

Create a class hierarchy that will represent trips, including member variables to store trip details. Create member functions that will:

  • compute the cost of the trip, and
  • display the details of the trip: origin, destination, distance and cost

Study the sample output carefully so you can correctly implement the trip hierarchy. 

Input

5
1·Zamboanga·Cagayan·494·5
1·Tagbilaran·Panglao·20·10
2·Cebu·Tagbilaran·90·800
3·Manila·Davao·1500·4500
3·Clark·Iloilo·751·2300

Output

Trip·Constructor
Bus·Constructor
Bus·Trip:·Zamboanga·to·Cagayan,·494·km,·P5·base·fare
Trip·Constructor
Bus·Constructor
Bus·Trip:·Tagbilaran·to·Panglao,·20·km,·P10·base·fare
Trip·Constructor
Boat·Constructor
Boat·Trip:·Cebu·to·Tagbilaran,·90·km,·P800·base·fare
Trip·Constructor
Plane·Constructor
Plane·Trip:·Manila·to·Davao,·1500·km,·P4500·base·fare
Trip·Constructor
Plane·Constructor
Plane·Trip:·Clark·to·Iloilo,·751·km,·P2300·base·fare
Total·Cost·of·trips:·10950








LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS