public class SquareTest {
public static void main(String[] args) {
System.out.println("Number of sides
is " + Square.NUM_OF_SIDES);
Square s1 = new
Square(-5.7f);
System.out.println(s1);
s1.setLength(0.001f);
System.out.println(s1.getLength());
s1.setLength(-9999);
System.out.println(s1.getLength());
System.out.println("Number of sides
is " + s1.NUM_OF_SIDES);
s1.calculateArea();
}
}
This topic appears to be stumping me. I now need to call the method to calculate the area and perimeter but when I try to invoke the method I get an error indicating incompatible type.The above code is what I have to call the method on. Not sure what I am missing here.
class Square
{
private float length;
public static final int NUM_OF_SIDES = 4; // static
constant value
//constructor
public Square(float length)
{
this.length = length;
}
//set and get methods
public void setLength(float length)
{
this.length = length;
}
public float getLength()
{
return length;
}
public float calculateArea()
{
return length*length;
}
public String toString()
{
return "Square : Length :
"+length+" Area : "+calculateArea();
}
}
class SquareTest {
public static void main(String[] args) {
System.out.println("Number of sides is " +
Square.NUM_OF_SIDES);
Square s1 = new Square(-5.7f);
System.out.println(s1);
s1.setLength(0.001f);
System.out.println(s1.getLength());
s1.setLength(-9999f);
System.out.println(s1.getLength());
System.out.println(s1);
}
}
Output:
Number of sides is 4 Square : Length : -5.7 Area : 32.489998 0.001 -9999.0 Square : Length : -9999.0 Area : 9.998E7
Do ask if any doubt. Please upvote.
public class SquareTest { public static void main(String[] args) { System.out.println("Number...
import java.util.Scanner; public class StudentClient { public static void main(String[] args) { Student s1 = new Student(); Student s2 = new Student("Smith", "123-45-6789", 3.2); Student s3 = new Student("Jones", "987-65-4321", 3.7); System.out.println("The name of student #1 is "); System.out.println("The social security number of student #1 is " + s1.toString()); System.out.println("Student #2 is " + s2); System.out.println("the name of student #3 is " + s3.getName()); System.out.println("The social security number...
Consider the following codes: public class TestThread extends Thread { public static void main(String[] args) { TestThread thread = new TestThread(); } @Override public void run() { printMyName(); } private void printMyName() { System.out.println("Thread is running"); } } Test Stem / Question Choices 1: What method should you invoke to start the thread TestThread? A: start() B: run() C: No. Thread will run automatically when executed. D: TestThread is not...
import java.util.Scanner; public class TriangleMaker { public static void main(String[] args) { // TODO Auto-generated method stub System.out.println("Welcome to the Triangle Maker! Enter the size of the triangle."); Scanner keyboard = new Scanner(System.in); int size = keyboard.nextInt(); for (int i = 1; i <= size; i++) { for (int j = 0; j < i; j++) { System.out.print("*"); } System.out.println(); } for (int...
What are the errors in this ? public class Mystery { public static void main(String[] args) { double initialSavings = 10000; double interestRate = 0.05; double currSavings = 0; int i; System.out.println("\nAnnual savings 5 years: "); currSavings = initialSavings; for (i = 0, i < 5, ++i); currSavings = (currSavings * interestRate); System.out.println("$" + currSavings); } }
Consider the following sample program: import java.util.Scanner; public class Palindrome { public static void main(String[] args){ Scanner kb = new Scanner(System.in); System.out.println("Enter a word:"); String word = kb.next(); String reverse = ""; for (int i=word.length()-1; i>=0; i--) reverse += word.charAt(i); boolean result = reverse.equalsIgnoreCase(word); if (result) System.out.println("The word " +word+ " is a Palindrome."); else System.out.println("The word " +word+ " is not a Palindrome."); } } Rewrite the program so that the main method is: public static void...
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...
Given the following Java code: public static void main (String[] args) { int num; System.out.println("Enter a number"); num = scan.nextInt(); <-first input statement while (num != 0) { System.out.println ("The number is: " + num); System.out.println("Enter a number"); num = scan.nextInt(); } //End While } //End Module The first input statement shown above is called the:
public class Q7 { public static void main(String[] args) { System.out.println("Math is fun"); //write your logic to continuously get one of the below options and print the result //1.reciprocal (1/x), 2.root (x^1/2), 3.cube (x^3), 4.Quit //get the number x as input from the user each time //hint:- x^n= Math.pow(x,n) } } need help in java
Correct the mistakes public class customer { public static void main(String[] args) { D customer=new d(); d.employee(56,”ali”); d.department(7,8.6,9); } public void employee(String name, int age) { System.out.println("Your name is"+name); System.out.println(“Age is”+age); } public int department(double d, double t, int a) { System.out.println("I have java exam at 3:00"); Return(1.1); } } V. PROGRAMMING. Write a complete JAVA program to implement the following. 1. Create a class named Exponent. Its main() method accepts an integer value from a user at the keyboard,...
public static void main(String[] args) { System.out.println("Welcome to the Future Value Calculator\n"); Scanner sc = new Scanner(System.in); String choice = "y"; while (choice.equalsIgnoreCase("y")) { // get the input from the user System.out.println("DATA ENTRY"); double monthlyInvestment = getDoubleWithinRange(sc, "Enter monthly investment: ", 0, 1000); double interestRate = getDoubleWithinRange(sc, "Enter yearly interest rate: ", 0, 30); int years = getIntWithinRange(sc, "Enter number of years: ", 0, 100); System.out.println(); ...