Write a java program
Convert an object into string
public String toString()
{ return area+”-“ + ln + “ –“ + nb;}
System.out.println(myphone);
-------------------myphone.toString()
Please find the required program along with its output. Please see the comments against each line to understand the step.
Note: The members of class is identified from the variables used in toString method in the question.
class Test {
public static void main(String[] args) {
Phone myPhone = new Phone(91,890,4699567); //create an object myPhone of class Phone
System.out.println(myPhone.toString()); //print the myPhone object
}
}
class Phone {
int area;
int ln;
int nb;
public Phone(int area, int ln, int nb) {
this.area = area;
this.ln = ln;
this.nb = nb;
}
@Override
public String toString() { //toString method : which will convert the object into string notation
return area+"-" + ln + "–" + nb;
}
public int getArea() {
return area;
}
public void setArea(int area) {
this.area = area;
}
public int getLn() {
return ln;
}
public void setLn(int ln) {
this.ln = ln;
}
public int getNb() {
return nb;
}
public void setNb(int nb) {
this.nb = nb;
}
}
--------------------------------
OUTPUT:
91-890–4699567
Write a java program Convert an object into string public String toString() { return area+”-“ +...
Write a java class definition for a circle object. The object should be capable of setting radius, and computing its area and circumference. Use this to create two Circle objects with radius 10 and 40.5, respectively. Print their areas and circumference. Here is the Java class file (Circle.java). Compile it. public class Circle{ //Instance Variables private double PI = 3.1459; private double radius; //Methods public Circle ( ) { } //get method (Accessor Methods ) public double getRadius (...
Convert this java code to scala
public class Main { public static String checkType (Object obj) { //check if the given obj is of type Person if (obj instanceof Person) { Person p = (Person) obj; //type cast to Person if (p.vehicle instanceof Car) { Car c = (Car) p.vehicle; return "This is a car with plate: " + C.plate; } else if (p.vehicle instanceof Truck) { Truck t = (Truck) p.vehicle; return "This is a truck with places: "...
JAVA public String toString() { return "Land Type: " + landType + "\n" + "Address: " + address + "\n" + "City: " + city + "\n" + "State: " + state + "\n"; } public boolean equals (Property obj) { if (landType == obj.get_landType() && address == obj.address && city == obj.get_city() && state == obj.get_state()) return true; else return false; } public Property copy () { Property copyOfObject = new Property(landType, address, city, state); return (copyOfObject); } }...
1. Write a Java program to count all words in a string. User inputs a string sentence and your program should count the number of words in that string. Test Data: Input the string: The quick brown fox jumps over the lazy dog. Expected Output: Number of words in the string: 9 2. Write a class called ATMTransaction. This class has a variable balance and account number. It has three methods, checkBalance, deposit and withdraws along with constructors getters and...
JAVA PROGRAM. Write a program to implement a class Clock whose getHours and getMinutes methods return the current time at your location. Also include a getTime method that returns a string with the hours and minutes by calling the getHours and getMinutes methods. Provide a subclass WorldClock whose constructor accepts a time offset. For example, if you livein California, a new WorldCLock(3) should show the time in NewYork, three time zones ahead. Include an Alarm feature in the Clock class....
(JAVA NetBeans)
Write programs in java
Example 9.13~9.15
//Book.Java
public class Book {
private String title;
private String author;
private double price;
/**
* default constructor
*/
public Book() {
title = "";
author = "";
price = 0.0;
}
/**
* overloaded constructor
*
* @param newTitle the value to assign to title
* @param newAuthor the value to assign to author
* @param newPrice the value to assign to price
*/
public Book(String newTitle, String newAuthor, double newPrice)...
In Java Create a testing class that does the following to the given codes below: To demonstrate polymorphism do the following: Create an arraylist to hold 4 base class objects Populate the arraylist with one object of each data type Code a loop that will process each element of the arraylist Call the first ‘common functionality’ method Call the second ‘common functionality’ method Call the third ‘common functionality’ method Verify that each of these method calls produces unique results Call...
This is for a java program public class Calculation { public static void main(String[] args) { int num; // the number to calculate the sum of squares double x; // the variable of height double v; // the variable of velocity double t; // the variable of time System.out.println("**************************"); System.out.println(" Task 1: Sum of Squares"); System.out.println("**************************"); //Step 1: Create a Scanner object //Task 1. Write your code here //Print the prompt and ask the user to enter an...
java
Use the classes given in the previous question and write the output tha t is generated when this program is run. You may write your answer as "Elaine 2 Jerry 1" or "Compiler Error" or "Runtime Error" If there is an error then explain why. public class Main{ public static void main(String args[]){ Object o = new Kramer(); ((George)(o)).method1(); ((Jerry)(o)). method1(); 0.method2(); public class George extends Elaine {! public void method1() { System.out.print("George 1 "); public class Jerry {...
Override toString() for the class HighScores. It should return a String that contains all of the high scores in entries, formatted nicely. Note that toString is already implemented for the Score class. Then, write a driver program (in a file named Main.java) that thoroughly tests HighScores (i.e. add and remove scores from the list, add scores to an already full list, make sure the list remains sorted, etc). Score.java public class Score { protected String name; // name of the...