Answers for "how to make a java class"

1

using class in java

class Number {
    	
    	int number;
    	
    	// square numbers
    	public boolean isSquare(){
    		
    		double squareRoot = Math.sqrt(number);
    		
    		
    		if(squareRoot == Math.floor(squareRoot))
    		{
    			return true;
    		} else {
    			return false;
    		}
    		
    	}
    	
    	// triangular numbers
    	public boolean isTriangular(){
    		
    		int x = 1;
    		
    		int triangularNumber = 1;
    		
    		while(triangularNumber < number){
    			
    			x++;
    			triangularNumber = triangularNumber + x;
    			
    		}
    		
    		if(triangularNumber == number){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    // testing 
    Number myNumber = new Number();
    myNumber.number = 16;
    
    System.out.println(myNumber.isSquare());
Posted by: Guest on March-11-2020
0

classes in java

public class Main {
  int x = 5;

  public static void main(String[] args) {
    Main myObj = new Main();
    System.out.println(myObj.x);
  }
}
Posted by: Guest on September-25-2021
1

using class in java

public class HelloWorld {
  public static void main(String[] args) {
    
    // how to use class in java
    class User{
    	
    	int score;
    	
    	// method that can be used outside the class
    	public boolean hasWon(){
    		if(score >= 100){
    			return true;
    		} else {
    			return false;
    		}
    	}
    }
    
    User dave = new User();
    
    dave.score = 20;
    
    System.out.println(dave.hasWon());
    
 
  }
}
Posted by: Guest on March-10-2020
1

how to make a class in java

// Public creates avaliability to all classes/files
public class Object {
  // Instance of a variable per each class created
  public int a = 1;
  // Private restricts in local space (within these brackets)
  private int b = 5;
  
  // Defaults as public
  int c = 0;
  
  // Method Examples
  void setB(int B)
  {
  	b = B;
    // No return b/c 'void'
  }
  int getA()
  {
    return b;
    // Return b/c 'int' in front of method
  }
}
Posted by: Guest on December-15-2019

Code answers related to "how to make a java class"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language