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); } }
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
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...
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(); ...
Review the following code: public class Looping { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= 5; j++) { System.out.println(i + " x " + j + " = " + (i * j)); } } } } What is the output from the code above? Replace this text with your solution What happens if you change the...
What output is produced by the following program? public class MysteryNums public static void main(String[] args) - int x = 12; int y = x - 3; 3, sentence (y, x + y); . public static void sentence (int numi, int num2) { 4: System.out.println(num1 + " + num2);